iOS: status bar and keyboard rotates after app in background

597 views Asked by At

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?

1

There are 1 answers

0
kr45ko On

You can try implementing application:supportedInterfaceOrientationsForWindow: in your App Delegate.

From Apple docs:

This method returns the total set of interface orientations supported by the app. When determining whether to rotate a particular view controller, the orientations returned by this method are intersected with the orientations supported by the root view controller or topmost presented view controller. The app and view controller must agree before the rotation is allowed. If you do not implement this method, the app uses the values in the UIInterfaceOrientation key of the app’s Info.plist as the default interface orientations.

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!