Question

I have a strange bug in my app when my UI flow is as follows:

  1. The mainViewController is launched in Portrait orientation, and in turn it displays a LoginViewController with modalPresentationStyle set to UIModalPresentationFullScreen.

  2. I turn the loginViewController to landscape mode, enter credentials to login (the methods to authenticate the user are defined in the mainViewController).

  3. The loginViewController is dismissed (from the mainViewController) and a bunch of buttons are loaded onto the screen to indicate that it's the home screen.

Now my problem is that the button positions appear as if the app were in Portrait orientation even though the app was switched to Landscape while the loginViewController was presented.

Moreoever, this happens ONLY when the modalPresentationStyle is set to UIModalPresentationFullScreen! When I present it as a form sheet or a page sheet, everything works normally (I, however, need my loginViewController to be displayed FullScreen).

So far I've tried manually calling the shouldAutorotate method when the loginViewController is dismissed etc., and that solves the problem but seems like a shoddy workaround rather than a fix.

I've also tried calling the shouldAutorotate method of the mainViewController from the loginViewController, but this changes nothing.

Any ideas on how to fix the issue?

Was it helpful?

Solution

Autorotation has changed in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientations: and shouldAutorotate methods:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}

Modal ViewControllers no longer get rotation calls in iOS 6: The willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are no longer called on any view controller that makes a full-screen presentation over itself—for example those that are called with: presentViewController:animated:completion:.

This answer will further explain the rotation changes in iOS 6


[EDIT] Completely different way is to register for rotation events. The advantage is that all objects can register for this, not only UIViewController. This is usually done when the view loads and stopped when the view disappears (put in dealloc here). The method in the selector is called when orientation changes (here it is orientationChanged:):

- (void)viewDidLoad {
    [super viewDidLoad];
    // Start generating device rotations and register for them
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)dealloc {
    // Deregister and stop generating device rotations
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [super dealloc];
}

OTHER TIPS

check this out.... may be this'll help...


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     return (interfaceOrientation == UIInterfaceOrientationPortrait |interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown| interfaceOrientation == UIInterfaceOrientationLandscapeLeft | interfaceOrientation == UIInterfaceOrientationLandscapeRight);

}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}

- (BOOL) shouldAutorotate
{
    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top