如何停止设备方向更改

时间:2017-04-24 07:16:43

标签: ios iphone swift orientation

我正在处理启动时处于纵向模式的应用。之后,我为我的项目添加了一些SDK。在集成过程中,SDK需要在“常规”窗格中选中“纵向”和“横向权限”。所以我按照SDK集成手册中的说明进行设置。

enter image description here

之后,当我在设备中运行项目并将其旋转到右侧时,应用设备方向正在发生变化。如何在不干扰SDK的情况下设置方向是固定的。

3 个答案:

答案 0 :(得分:3)

如果您在应用启动后立即无法设置方向,则可能需要停止每个视图控制器或其中一个基本导航控制器的方向

斯威夫特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
}}

使用YourBaseNavigationController作为导航控制器。基本上,此导航控制器中的所有视图控制器都将强制仅支持纵向模式。

答案 1 :(得分:2)

取消选中支票横向左侧和横向右侧

答案 2 :(得分:0)

您需要通过代码禁用方向。

在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
}