23

I have a UIViewController with the following code:

- (BOOL) shouldAutorotate {
     return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

I am not using a UINavigationController. When this UIViewController is being displayed, the device will still rotate to landscape. I am targeting iOS 9, what's the issue here?

Bryan
  • 3,199
  • 3
  • 16
  • 24

5 Answers5

36

So the issue was that I had defined the allowed orientations in info.plist which apparently overrides anything you do anywhere else throughout the project.

To correct the issue I removed the entries from info.plist and defined them in the project settings. Now everything works as expected.

Bryan
  • 3,199
  • 3
  • 16
  • 24
  • Thanks ! you saved my day. For the record, i've had this bug on ios 9.0, but it seems to be fixed on ios 9.1 – Ben G Oct 03 '15 at 20:18
  • 3
    Bryan, I removed the entries from the info.plist file, but I don't understand what you mean by defining them in the project? I still have all my VCs changing orientation. Even if I added the methods to the VC to prevent it (supportedInterfaceOrientations, shouldAutoRotate, etc). I'm trying to tackle allowing landscape mode chunk at a time, and need to prevent landscape mode for the VCs that need extensive refactoring. – stulevine Oct 07 '15 at 13:06
  • Totally saved my day, thank you! Deleting the supported interface orientations in the Info.plist and then just implementing shouldAutorotate and supportedInterfaceOrientations fixed this for me. – user2393462435 Oct 15 '15 at 16:09
  • 6
    Note that removing orientation support from info.plist will cause an error on upload (ITMS 90474) for iOS9 see: http://stackoverflow.com/questions/32728711/xcode-7-error-itms-90474-invalid-bundle-cant-submit-to-apple – Tico Ballagas Oct 22 '15 at 00:00
  • 5
    When you remove the orientations in `info.plist` it also changes the project settings. – mrhangz Feb 11 '16 at 21:10
  • 1
    @TicoBallagas That's a red herring, because as your link shows, and the above answer already stated, setting 'Requires Full Screen' will fix any errors on upload. What it will not to, and what everyone needs, is for the method to be called. I have `shouldAutorotate` implemented in several view controllers, yet none of them get called. XCode 8.2, iOS 10.2 – Logicsaurus Rex Feb 02 '17 at 19:15
25

I don't think Bryan's answer works,for changing the orientations in project settings also changes the info.plist as @mrhangz commented.

If the issue is iOS9 only,it is probably due to the new feature of iOS9 in iPad called Split view.The iOS9 enable Split view by default in particular iPad device,see Apple documents here.

enter image description here The split view forced your app to support all orientations in all view once adoptted.So if you set all orientations support in either info.plist or target general setting,and then split view is supported by default,which will ignore the orientation setting though supportedInterfaceOrientations in your viewController and support all orientations.

As the document written,if you checked Requires full screen in your target settings,then your app will not support split view.Now you can control orientations in code again. enter image description here

Xingxing
  • 580
  • 1
  • 6
  • 17
  • This works for me. I needed this info, I do not know it. – 93sauu Oct 24 '16 at 12:48
  • 2
    Didn't work for me. I have `-(BOOL)shouldAutorotate` in my view controller, but it never gets called, and I have 'Requires Full Screen' checked. Always has been checked as far as I know. – Logicsaurus Rex Feb 02 '17 at 19:11
  • Using Xcode 9 and testing on iPhoneX, I am seeing same issue. The overrides seem to have no effect and the suggestions about plist mods and requiring full screen do not effect the outcome in my testing. The only things that seems to effect the outcome is locking down the rotations in the settings which applies app wide. Wondering if anyone found the "secret" to getting this work, or it's still a mystery. – bduhbya Jan 26 '18 at 01:58
0

I have try many solution, but the correct answer with working solution is:

ios 8 and 9, no need to edit info.plist.

- (BOOL) shouldAutorotate {
 return NO;
 }   



- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait |  UIInterfaceOrientationPortraitUpsideDown);
 }

possible orientation

UIInterfaceOrientationUnknown

The orientation of the device cannot be determined.

UIInterfaceOrientationPortrait

The device is in portrait mode, with the device held upright and the home button on the bottom.

UIInterfaceOrientationPortraitUpsideDown

The device is in portrait mode but upside down, with the device held upright and the home button at the top.

UIInterfaceOrientationLandscapeLeft

The device is in landscape mode, with the device held upright and the home button on the left side.

UIInterfaceOrientationLandscapeRight

The device is in landscape mode, with the device held upright and the home button on the right side.

hoogw
  • 4,982
  • 1
  • 37
  • 33
  • 4
    You need to return UIInterfaceOrientationMask, not UIInterfaceOrientation to the supportedInterfaceOrientations (e.g. UIInterfaceOrientationMaskPortrait) – Aaron Zinman Mar 22 '17 at 23:13
0

In swift 5

The code below will lock the current view controller into portrait mode but still allow the other view controllers to transition to landscape. I do believe that you have to enable all the orientations at the project level and then turn then "off" using this method but am not sure if there is way to turn them back "on" one by one.

private var _orientations = UIInterfaceOrientationMask.portrait
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get { return self._orientations }
    set { self._orientations = .portrait }
}

A more thorough explanation of it all can be found here: The supportedInterfaceOrientations method doesn't override any method from its superclass

Daniel Patriarca
  • 361
  • 3
  • 20
0

For simplicity, for iPad, if Supported interface orientations (iPad) property in info.plist includes all the four orientations, with UIRequiresFullScreen property value as NO, iOS will treat your app as supporting split view. If an app supports split view feature, you can not disable it from rotating, at least by the ways above.

I have a detail answer here.

HongchaoZhang
  • 3,494
  • 1
  • 17
  • 8