Levi
Levi

Reputation: 7343

Device orientation change issue

My viewController1 has a child view controller (viewController2). viewController2.view is a subview of viewController1's view. At the user's action, I remove the view of viewController2 from it's superview. After a while I have to add it again as subview.

The problem is that if the user rotates the device while viewController2 is not visible, after adding it again to viewController1's view, it's frame and it's subviews are placed as the device was still in the old orientation. Even the viewController2.view.frame has the height and with interchanged.

Does anyone have a solution to this? Thanks in advance!

Upvotes: 0

Views: 139

Answers (2)

lnafziger
lnafziger

Reputation: 25740

Without seeing your code, I would guess that you are keeping a reference to view controller 2 even when it isn't visible. In that case, you need to forward the view rotation events to the controller so that it knows about the rotation like this (Do this for each event that you want to forward):

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    // Forward to view controller 2 if it is not displayed
    if (!viewController2.view) {
        [viewController2 willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
}

A better design may be to just set view controller 2's view to hidden instead of removing it, and it will still get the events without manual intervention.

Upvotes: 2

Sakares
Sakares

Reputation: 606

Let me guess your problem. Do you want to show the different UIView base on orientation , right?

It's about update your viewController2 to load the nib base on orientation. You can make the landscape and portrait nib to support your different UI due to orientation. Try to look from this Answer

Hope it helps you

Upvotes: 0

Related Questions