简体   繁体   中英

How to stop the device Orientation changing

I'm working on an app that is to be in portrait mode when it is launched. After that, I add an SDK for some purpose to my project. In the process of the integrating the SDK needs both Portrait and Landscape Right checked in the General pane. So I set it as given in the SDK integration manual.

在此处输入图片说明

and after that when I run my project in a device and rotate it to right the app device orientation is changing. How can I set the orientation is fixed without disturbing the SDK.

if you're having trouble to set orientation right after the app launches then you might need to stop orientation for each view controller or one of it's base navigation controller

Swift 3:

class YourBaseNavigationController: UINavigationController {
override func viewDidLoad() {
    super.viewDidLoad()
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
override var shouldAutorotate: Bool {
    return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}}

Use YourBaseNavigationController as your navigation controller.Basically all view controller within this navigation controller will be force to support portrait mode only.

取消标记支票左横向和右横向

You need to disable orientation through code.

Add this code in your RootViewController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);//set 'return NO'; for stop orientation
}

Swift :

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

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