Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I notify system when supportedInterfaceOrientations changes?

My root view controller's implementation of supportedInterfaceOrientations almost always returns UIInterfaceOrientationMaskAll, however there is one edge case where it returns UIInterfaceOrientationMaskLandscape.

This is working, if the user rotates the device. However if the device is being held in portrait mode the supportedInterfaceOrientations method does not ever get called, unless the user manually rotates the device.

How can I programatically tell the system that the return value of this method has changed?

According to the documentation, it seems like I should be able to call [UIViewController attemptRotationToDeviceOrientation] however this does not have any effect (supportedInterfaceOrientations is never called and the screen does not rotate).

I found various workarounds others have posted to try and solve this problem, but none of them work in my tests. I suspect they may have worked in iOS 5.0, but not iOS 6.0.

I am returning YES in the root view controller's shouldAutorotate method.

like image 558
Abhi Beckert Avatar asked Oct 31 '12 01:10

Abhi Beckert


1 Answers

First of all, it might be useful if you used this if you want to present your UIViewController in Landscape mode.

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

Also, A lot depends on with which controller is your UIViewController embedded in.

Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.

subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) needs to be set it as self.window.rootViewController.

- (BOOL)shouldAutorotate
 {
     return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
     return self.topViewController.supportedInterfaceOrientations;
 }

From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.

Note :

The shouldAutorotate and supportedInterfaceOrientations method always get called for UINavigationController whenever Push operations are done.

like image 135
mayuur Avatar answered Oct 08 '22 04:10

mayuur