开发者

UIView addsubview after orientation change: Tell view to resize

开发者 https://www.devze.com 2023-03-05 13:28 出处:网络
I have a UIView as a XIB in Portrait mode. This view is added programmatically to the viewcontroller like this:

I have a UIView as a XIB in Portrait mode.

This view is added programmatically to the viewcontroller like this:

NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"InputView" owner:self options:nil];
    InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
    [self.view addSubview:inputView];

This view has autoresizing masks set up properly and rotates fine when the orientation changes from Portrait 开发者_StackOverflow社区to landscape.

However, if the orientation is already landscape and I create the view after the orientation change, it has its initial portrait orientation.

Is there a way to tell the view to initialize or resize itself to portrait by using its masks?

Thanks in advance for any reply!

EDIT: Using the suggestions of occulus and Inder Kumar Rathore (thanks guys!), I altered the code to this:

InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
[self.view setNeedsLayout];
[self.view layoutSubviews];
[self.view layoutIfNeeded];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
[self shouldAutorotateToInterfaceOrientation:orientation];

Unfortunately, there is no change at all. I think I found someone asking the same question:

When adding a sub view the view does not resize if the app is in landscape mode

The answer identifies the problem correctly, but is not very encouraging... Sure, I could create two nibs or resize the frame, but this seems so contrary to the idea of auto-resizing. I find it hard to believe that there is no way to tell a nib after awakening and adding it to a view to use its autoresize features...something that works flawless when the device rotates.

EDIT 2:

The solution of idz works:

InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
inputView.frame = self.view.bounds;
[inputView show];

Thanks!


Often a NIB/XIB file contains a UIViewController that takes care of all of this. In this case, since their is no view controller (in the NIB/XIB) you need to take over its post-load duties.

Calling layoutSubviews directly, or indirectly via setNeedsLayout or layoutIfNeeded won't do you much good because the default implementation does nothing.

Assuming you want input view to fill the bounds of self.view you do the following:

InputView *inputView = (InputView*)[nibObjects objectAtIndex:0];
[self.view addSubview:inputView];
inputView.frame = self.view.bounds;
[inputView show];

All the resize masks of the sub-views must be correctly set for this to work and, obviously, if you don't want to fill the full bounds you may want to adjust the frame.


[self.view addSubview:viewSpinner];
viewSpinner.frame = self.view.frame;
[viewSpinner setNeedsLayout];

This works for me (Y)


I don't know if you still have this issue.

Let's say you have the following architecture:

  • window
    • subviewcontroller

(you implemented shouldautorotate correct to answering the wanted orientations)

Into this subviewcontroller you want to add the views of new UIViewControllers by just calling the addSubview function.

Instead of implementing the bounds manipulation in shouldautorotate, you should implement it in

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
  self.newUIViewController.view.frame = self.view.bounds;
}

didRotateFromInterface... is called after shouldRotate. In this function the bounds are already setup correctly.

This way you don't need so much manipulation by code.


See this related question:

When do autoresizing masks take effect in iOS?

So after loading your new view from the nib, and adding as a subview to self.view, try calling setNeedsLayout.

0

精彩评论

暂无评论...
验证码 换一张
取 消