1
votes

I have a UINavigationController which's view is embedded into UIViewController. When I rotate the device, NavigationController perfectly received the willRotate didRotate events. However, any UIViewControllers pushed before the current visible view controller inside UINavigationController does not receive rotation events.

Here is the problem: While the second viewController is visible in the navigationController, if I rotate the device, ViewController changes it's layout according to the rotation. When I pop the viewcontroller to get back to first view controller, first view controller shows itself according to portrait layout as It doesn't call my re-layouting methods for the rotated view.

I don't want to add observers for rotation change because it normally already behaves correctly as default rotation methods are automatically called normally...

What can I do in this situation?

I have done it by manually calling willRotate function but I don't feel that this is good.

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    UIInterfaceOrientation io=[[UIApplication sharedApplication] statusBarOrientation];

    [self willRotateToInterfaceOrientation:io duration:0.3];

    for(int i=0;i<self.childViewControllers.count;i++){
        [[self.childViewControllers objectAtIndex:i]willRotateToInterfaceOrientation:io duration:0.3];
    } 
}
2

2 Answers

0
votes

the code you mentioned is not correct.. we should not call call a delegate method explicitly. please implement the below function in the viewControllers you want to have auto rotate feature in. You can also restrict rotations based to the orientations u want by putting a check for permitted orientations.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
       return YES;
} 
0
votes

I don't think you need any rotation event observers. However, the UIViewController instances in the UINavigationController need to be notified about layout changes. This happens automatically if you add your UINavigationController to the view controller hierarchy:

[yourParentViewController addChildViewController:yourNavigationController];

This will make sure that your navigation controller gets notified about layout changes, and will pass this information on to ALL its view controllers.

Then you can simply do your layout in the UIViewController's viewDidLayoutSubviews method (or if you have a subclassed view, in its layoutSubviews method.

Note: This method requires iOS 5 or higher!