7
votes

As the title says. My UIViewController will not rotate no matter what. When it loads shouldAutorotateToInterfaceOrientation is being called but after that it doesnt.

UPDATE 1:

It's a really really wierd problem. At least for me. And i ll try to explain everything.

It's a navigation based app. Every controller has

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

Xcontroller is a child of Acontroller and it doesn't auto rotate. If Xcontroller become a child of Bcontroller then it will autorotate. So something is wrong with Acontroller. But Acontroller is identical (except its data) to Bcontroller.

Whats Wrong?

UPDATE 2:

I decided to recreate Acontroller. And it worked.I believe I was missing something stupid.

9
How did you implement the -shouldAutorotateToInterfaceOrientation:? - kennytm
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; } If that is what you mean :). - looneygrc

9 Answers

15
votes

I am not sure whether it's the same reason as your case. But I experienced the same thing. the shouldAutorotateToInterfaceOrientation was only called once in the beginning. After some serious debugging by taking code apart, I found that the reason is in my overridden init method. I had this before:

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self) {     
        self.photoAlbum = theAlbum;     
    }
    return self;
}

And then I changed to this

- (id)initWithAlbum:(PhotoAlbum *)theAlbum {
    if (self = [super init]) {      
        self.photoAlbum = theAlbum;     
    }
    return self;
}

Note: the only difference is I added [super init] to call the parent init. After this change, the rotation works well and the shouldAutorotateToInterfaceOrientation is being called everytime I rotate the screen. Hope this help.

8
votes

There can be several possible reasons your view controller does not rotate.

See Apple's official Q&A on this issue:

Why won't my UIViewController rotate with the device?

http://developer.apple.com/library/ios/#qa/qa2010/qa1688.html

5
votes

Apple Q&A has the detailed solution for the problem.

Why won't my UIViewController rotate with the device?

http://developer.apple.com/library/ios/#qa/qa1688/_index.html

If you add a viewcontroller.view to uiwindow, you should set this viewcontroller as rootviewcontroller.

[self.window addSubview: mainViewcontroller.view];
self.window.rootViewController=mainViewcontroller;
3
votes

Also, make sure you don't have rotation lock on. I spent a good hour trying to figure out why my views stopped rotating. shouldAutorotateToInterfaceOrientation was being called only once at start up and when Game Center leaderboards/achievements were presented.

3
votes

I had the same issue - the reason was, that it was my first UIViewController, that i created on the fly in my ApplicationDelegate, added it's View to my UIWindow and immediately released it.

That's of course not correct as I just added the UIView of the UIViewController (retaining it) and than released the whole controller.

You should add your first UIViewController as an instance variable in Your ApplicationDelegate instead, and release it in Your ApplicationDelegate's dealloc-method.

1
votes

In my case, the ViewController was inside a NavigationController which was used by a "parent" viewControlled that received the orientation changes.

What I did in this parent was:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{

    if(_navigationController){
        return  [_navigationController.topViewController shouldAutorotateToInterfaceOrientation: toInterfaceOrientation];
    }

    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

This way you can implement your own orientation change logic depending on the currently visible controller.

0
votes
  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES; } The above method if u using, you will able to call many time if u want with out any error.
0
votes

I think there is no strange behavior here, it is called only one which is right. There is no need to call more than one to decide if the device should rotate to a direction or not.

This method just ask if the device should rotate to a direction or not. If you want to handle the orientation change, you should register for the notification from the UIDeviceDidChangeOrientationNotification and override the following method:

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
    !isShowingLandscapeView)
    {
        [self presentModalViewController:self.landscapeViewController
                            animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait &&
             isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }
}

See more here.

0
votes

I have the same problem but with two view controllers added to the application's UIWindow. The reason is The view controller's UIView property is embedded inside UIWindow but alongside an additional view controller

From Apple Technical Q&A

http://developer.apple.com/library/ios/#qa/qa1688/_index.html