4

I have created an app with many views and I want to have some of them only in portrait orientation. I have coded this in .m file:

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

Do I need to do something else? May be in .h file?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Ash Var
  • 241
  • 2
  • 3
  • 12

5 Answers5

5

Cleanest solution for me:

Go to Info.plist file and for the "Supported interface orientations", remove every value except "Portrait (bottom home button)".

da Rocha Pires
  • 2,443
  • 1
  • 24
  • 19
4

You just need to return a BOOL in that method. If you want just portrait mode, that means:

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

If it's fine to be also Portrait Upside down (when in portrait rotate the device 180 degrees), then the method will look like:

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

The last condition can be replaced with a call to UIDeviceOrientationIsPortrait(interfaceOrientation), which does the same comparison (cf: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html)

LE: If this doesn't work, you can try using the follow 'hack': try to pop and push the view again (if you're using NavigationController). You can use the popViewControllerAnimated and pushViewController:animated: methods to force the controller re-query the required orientation :) Source: http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html)

Timotei
  • 1,909
  • 2
  • 22
  • 31
  • It *has* to work. Please check if: a) you override the method in the right class (an UiViewController) b) the method has the right signature (name, params) – Timotei Jul 23 '12 at 17:38
  • 1
    What iOS version are you using? In iOS6 things have changed a bit. Take a look here: http://stackoverflow.com/a/12555205/75320 and http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html – Timotei Jan 13 '13 at 09:40
  • Hi, please check my latest edit, to see if it works that way. – Timotei Mar 26 '13 at 12:51
2

You have to return YES for the orientations you support (portrait), not simply NO for everything. Also make sure that in your project's target's settings you only check portrait mode as supported.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • I have horizontal and portrait orientation checked in project. I want only a few screens to be only portrait. And others to be in both port. and for. orientation. – Ash Var Jul 13 '12 at 09:51
1

add following methods to your viewcontroller. This will ensure only portarait mode is supported for example.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
blacklion9279
  • 45
  • 1
  • 8
0

Try this..

 -(BOOL)shouldAutorotate
 {
  return NO;
 }

-(NSUInteger)supportedInterfaceOrientations
 {
 return UIInterfaceOrientationMaskPortrait;
 }

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