如何停止设备方向改变
How to stop the device Orientation changing
我正在开发一款在启动时将处于纵向模式的应用程序。之后,出于某种目的,我将 SDK 添加到我的项目中。在集成 SDK 的过程中,需要在 General 面板中勾选 Portrait 和 Landscape Right。所以我按照SDK集成手册中给出的设置。
之后,当我 运行 我在设备中的项目并将其向右旋转时,应用程序设备方向正在改变。如何在不打扰SDK的情况下固定方向
如果您在应用启动后无法立即设置方向,那么您可能需要停止每个视图控制器或其中一个基本导航控制器的方向
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
}}
使用 YourBaseNavigationController 作为导航 controller.Basically 此导航控制器中的所有视图控制器将强制仅支持纵向模式。
您需要通过代码禁用方向。
在您的 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
}
取消勾选横向左侧和横向右侧
我正在开发一款在启动时将处于纵向模式的应用程序。之后,出于某种目的,我将 SDK 添加到我的项目中。在集成 SDK 的过程中,需要在 General 面板中勾选 Portrait 和 Landscape Right。所以我按照SDK集成手册中给出的设置。
之后,当我 运行 我在设备中的项目并将其向右旋转时,应用程序设备方向正在改变。如何在不打扰SDK的情况下固定方向
如果您在应用启动后无法立即设置方向,那么您可能需要停止每个视图控制器或其中一个基本导航控制器的方向
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
}}
使用 YourBaseNavigationController 作为导航 controller.Basically 此导航控制器中的所有视图控制器将强制仅支持纵向模式。
您需要通过代码禁用方向。
在您的 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
}
取消勾选横向左侧和横向右侧