18

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.

Trott
  • 66,479
  • 23
  • 173
  • 212
Dror Sabbag
  • 375
  • 2
  • 4
  • 16
  • What devices and/or simulator versions are you seeing this on? What is the type of your root navigation or tab bar controller? – warrenm May 19 '10 at 20:11
  • Xcode 3.2.2, iphone simulator 3.1.3, (ipad 3.2) and using the device 3.1.3 as well. I have both Tab Bar and Navigation controller in my mainWindow Nib file. but all my classes are subclasses of UIViewController – Dror Sabbag May 19 '10 at 20:13

5 Answers5

44

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.

cduhn
  • 17,818
  • 4
  • 49
  • 65
  • 4
    "A UITabBarController will only autorotate if all of its view controllers are set to autorotate." --- gets me every time; this is simply the wrong way for Apple to implement the API. Hopefully they'll fix it one day. – Adam Sep 17 '11 at 15:50
  • +1 this will concisely and completely explain the custom behavior to anyone making a "normal app" with nav- and tabbarcontrollers - it did for me and it works. As a side note, an app built with navcontrollers inside a tabbar in IB will get the correct rootcontroller, and the default struts, autosize, and view orientation settings when creating the viewcontrollers are fine, so no need to set or correct any of those. – Henrik Erlandsson Mar 22 '12 at 07:37
  • "A UITabBarController will only autorotate if all of its view controllers are set to autorotate." !!!! (bears repeating :). Seems like perhaps this isn't the case for iOS 6 and above, but is for iOS 5 (was very confused by the behavior). – Tim Arnold Mar 10 '13 at 14:56
2
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    UIInterfaceOrientation des=self.interfaceOrientation;

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
    {
        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portairait
        {

        }
        else//ipad -landscape
        {

        }
    }
    else//iphone
    {
        UIInterfaceOrientation des=self.interfaceOrientation;

        if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
        {

        }
        else //iphone -landscape
        {

        }
    }
return YES;
}
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
1

Which iOS are you building for? It was deprecated in iOS 6.0. (You should override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.)

Also you can call shouldAutorotate on the UIViewController class:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/shouldAutorotate


Ensure you have checked the Supported Interface Orientations within the "Summery" tab of the project settings (Select the project name in the 'Project Navigator' at the very top).

If you have not selected the orientations you want to use here, then the simulator/iphone will not allow the screen to change orientation.

Rob
  • 1,233
  • 13
  • 24
0

I had this problem but it worked in iOS6 but not iOS5. Turns out I had a view in my storyboard that I hadn't made a viewcontroller class for yet.

Tom Kincaid
  • 4,887
  • 6
  • 47
  • 72
0

...and last but not least, make sure you haven't activated the setting "Portrait Orientation Locked" on your test device (of course doesn't apply to the simulator), this will disable rotating in any app no matter what shouldAutorotateToInterfaceOrientation returns.

newenglander
  • 2,019
  • 24
  • 55