Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView addsubview after orientation change: Tell view to resize

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 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!

like image 227
marimba Avatar asked May 13 '11 17:05

marimba


2 Answers

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.

like image 118
idz Avatar answered Nov 11 '22 23:11

idz


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

This works for me (Y)

like image 33
yasirmturk Avatar answered Nov 11 '22 21:11

yasirmturk