willRotateToInterfaceOrientation not being called from presented viewcontroller

IgnazioC picture IgnazioC · Jan 5, 2013 · Viewed 10k times · Source

I have uiviewcontroller on ipad with this configuration:

shouldAutorotate (true) supportedInterfaceOrientations (UIInterfaceOrientationMaskAll) and inside willRotateToInterfaceOrientation i perform some trick to adjust my interface.

From a child of this controller I show a QuickLookController with this -poor- code.

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:previewController animated:YES completion:nil];

But if I rotate my ipad the method willRotateToInterfaceOrientation not being called, So I cannot do the trick to adjust the interface.

Someone can explain me or given me some advices? thanks

Answer

Moin Ahmed picture Moin Ahmed · Jan 5, 2013

Reason : There may be many possibilities to this problem.

1) If your view's viewController is a subView of some other rootViewController which is not a navigationController, then there might be chances that rotation call is not propagating to the subView's controller.

2) Somewhere I read that if Super methods are not called properly where it is needed then it might be the cause of rotation problem, which means that all ViewControllers in view stack which are related to the autorotation must call the super methods in method implementations (i.e. calling [super viewDidLoad] from the ViewController's viewDidLoad).

You can use below trick to handle orientation changes.

Register a notifier in viewWillAppear.

-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];}

The orientation change will notify the below function.

- (void)orientationChanged:(NSNotification *)notification{
[self handleOrientation:[[UIApplication sharedApplication] statusBarOrientation]];}

which will call the below method where you can handle the orientation changes.

   - (void) handleOrientation:(UIInterfaceOrientation) orientation {

        if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
        { 
            //handle the portrait view    
        }
        else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) 
        {
            //handle the landscape view 
        }
     }