以下是一些先决条件。
第一
我们为应用程序选择所需的方向:
第二
我们需要在info中添加一些参数。plist,检查一下
Supported Interface Orientations
包含在第一步中选择的方向。
现在进入编码部分:
在Appdelegate类中,将此代码添加到以下函数:
func applicationDidBecomeActive(_ application: UIApplication)
//case 1: for view controllers not part UINavigationController in your.storyboard
if(self.window?.rootViewController?.presentedViewController is GraphViewController){
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
//or
//case 2: for view controllers which are part UINavigationController in your.storyboard
let currentVC = self.window?.rootViewController as? UINavigationController
if(currentVC?.viewControllers.last is GraphViewController){
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
就是这样,现在运行应用程序后,如果应用程序被发送到后台(
GraphViewcontroller处于活动屏幕时
)按下主页按钮,然后从应用程序托盘中选择返回应用程序
GraphViewcontroller
将保留方向。