8

For iOS 10 and below, the following code controlled the orientation of the any respective UIViewController. I have selected Portrait, Landscape Left, and Landscape Right in my Deployment Info, and have the following in my Info.plist:

enter image description here

For my VC's that should not rotate at all I have the following code, which I stated, was working prior iOS 11

- (BOOL)shouldAutorotate {
    [super shouldAutorotate];
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    [super supportedInterfaceOrientations];
    return UIInterfaceOrientationMaskPortrait;
}

I have tested this on actual devices and as of iOS 11 it does not work.

Even more strangely, logging the registered device orientation as of iOS 11 tells my the device IS portrait... when the controller loads in landscape mode...

Code:

// In viewDidLoad 
NSLog(@"orientation: %lu", [[UIDevice currentDevice] orientation]);

Console output:

2017-09-22 15:20:26.225196-0400 <APP_NAME>[2669:1628408] orientation: 1

This occurs either rotating the device left or right before building and running the app.


  • What is the cause here for this error? If anyone knows please help..

FYI: No, I do not have rotation lock on my device..

Community
  • 1
  • 1
Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42

1 Answers1

6

Whether this proves to be an iOS 11 bug or not, I seemed to have stumbled upon a "solution" to this issue. For whatever reason, for iOS 11 changing the - (BOOL)shouldAutorotate return to YES allows for the correct orientation...

Objc

- (BOOL)shouldAutorotate {

    [super shouldAutorotate];
    
    if (@available(iOS 11, *)) return YES;
    return NO;

}

In combination I had to do a manual check for the screen dimensions to see if the width was greater or less than the supposed height of the screen.

width = self.view.frame.size.width, height = self.view.frame.size.height;
if (height < width) width = height, height = self.view.frame.size.width;

Hopefully someone else finds the true cause of this "bug" OR Apple updates their bundle to handle rotations like all previous iOS versions..

Swift 3.2+

override var shouldAutorotate: Bool {

    if #available(iOS 11.0, *) {
        // Anything else iOS 11 specific
        return true
    } 
    return false

}
Community
  • 1
  • 1
Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42
  • 2
    On iOS 11.2 - (BOOL)shouldAutorotate {} is not called at all. Not in the root view controller, nor in later view controllers. – RickJansen Feb 18 '18 at 16:26
  • Create a navigation controller and call this method there if your VC is not calling - https://stackoverflow.com/questions/40413567/overriding-shouldautorotate-not-working-in-swift-3 – Will Von Ullrich Feb 18 '18 at 17:03
  • I found that iOS 11 wouldn't call any of these methods until I had implemented `application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)` in my `UIApplicationDelegate` class. – Beltalowda Apr 13 '18 at 02:14