shouldAutorotateToInterfaceOrientation doesn't work

Dror Sabbag picture Dror Sabbag · May 19, 2010 · Viewed 35.8k times · Source

I've been writing my Universal application in portrait mode, and now after about 15 nib files, many many viewCotnrollers, I'd like to implement the shouldAutorotateToInterfaceOrientation and design some screens in Landscape mode.

adding :

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

to ALL of my viewControllers, does not do the work.

During Debug, i see that this method is called, but it just won't work! not in the simulator, not in the device, not in Iphone, not in Ipad!

i've searched some answers in the forum, and saw some advises to use:

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

Didn't worked either,

adding:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

and

 [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

to my viewDidLoad and viewDidUnload respectively didn't worked either.

I'm lost.. Any help will do!

just one more info... all my Views are of type UIControl, as i needed the TuchUpInside to work.

Appriciate your help.

Answer

cduhn picture cduhn · May 20, 2010

Make sure all of your parent views have autoresizesSubviews = YES. You may need to do this in code if you haven't set springs and struts in IB for all of your views.

Quoting the Interface Builder User's Guide:

Important: In a Cocoa nib file, if you do not set any springs or struts for your view in Interface Builder but then do use the setAutoresizingMask: method to add autosizing behavior at runtime, your view may still not exhibit the correct autoresizing behavior. The reason is that Interface Builder disables autosizing of a parent view’s children altogether if those children have no springs and struts set. To enable the autosizing behavior again, you must pass YES to the setAutoresizesSubviews: method of the parent view. Upon doing that, the child views should autosize correctly.

A couple other things to be aware of:

  1. A UINavigationController will only autorotate if its root view controller is also set to autorotate.

  2. A UITabBarController will only autorotate if all of its view controllers are set to autorotate.