I have an app which is 99 % portrait only, just one viewcontroller is in landscape. Therefore my Info.plist contains three possible orientations: Portrait and both Landscape.
Everything works fine, except: when I bring my app to the background and to the front again, the status bar (and, if showing, the keyboard) rotate to landscape when I rotate the device. My views remain correct.
Is this a bug or am I missing something?
You can try implementing
application:supportedInterfaceOrientationsForWindow:in your App Delegate.From Apple docs:
You can then add something like this (assume your just-one-landscape-controller is MyLandscapeOnlyViewController):
Objective-C
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { // Return the desired orientation for your custom-oriented ViewController if ([window.rootViewController.presentedViewController isKindOfClass:[MyLandscapeOnlyViewController class]]) { return UIInterfaceOrientationMaskLandscape; } // Default orientation is portrait return UIInterfaceOrientationMaskPortrait; }Swift 3
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { // Return the desired orientation for your custom-oriented ViewController if (window?.rootViewController?.presentedViewController?.isKind(of: MyLandscapeOnlyViewController.self) == true) { return .landscape } // Default orientation is portrait return .portrait }The inspection of the topmost presented ViewController might be more challenging depending on your view hierarchy but this is the general idea.
Hope this helps!