简体   繁体   中英

How to disable autorotation for UINavigationController's root view controller?

I need to support all interface orientation masks in my App. Some view controller shouldn't autorotate(it supports Portrait orientation only), but the App still need to support all orientations. Expected result

I tried to set shouldAutorotate = true; supportedInterfaceOrientations = All shouldAutorotate = true; supportedInterfaceOrientations = All in UINavigationController and shouldAutorotate = false; supportedInterfaceOrientations = Portrait shouldAutorotate = false; supportedInterfaceOrientations = Portrait in root view controller. But it's not working.

You can use this in viewControllers that support only Portrait mode

override func shouldAutorotate() -> Bool {
    if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
            return true;
    } else {
        return false;
    }
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}

Since every view controller supports a different orientation, you can do this:

1) In the General tab of your project, select the orientations you need your application to support. for reference click here

2) Now, add the following code in each view controller:

   - (UIInterfaceOrientationMask) supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait;
}

Return whichever orientation you want for that specific view controller in this method.

You have to set Xcode settings for handling rotation as following steps for that:

Project> Targets> General> Deployment Info> Device Orientation> (check mark) Portrait

Hope this is helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM