ios-从后台状态恢复后保持特定活动 ViewController 的屏幕方向

ios-to maintain screen orientation of a particular active ViewController after resuming from background state

对于我的应用程序中的图形视图屏幕,方向必须是横向模式,而对于其他屏幕,方向必须是纵向模式。

问题是当图表视图在活动屏幕上并且应用程序进入后台时。
当应用程序 returns 回到活动状态时,图表的方向更改为纵向模式,这是不受欢迎的。
有什么办法可以保留图形视图的方向?
注意:图形视图控制器未与任何导航控制器连接,并在按钮单击事件后以编程方式调用。

提前致谢。

有一种方法可以锁定方向。可能会解决你的问题。

我发现这个答案使事情变得更容易,而不是在这里再写一遍。

解法:.

希望对您有所帮助!

这里有一些先决条件。

首先我们选择应用所需的方向:

其次我们需要在info.plist中添加一些参数,检查Supported Interface Orientations是否包含第一步选择的方向。



现在进入编码部分: 在 Appdelegate class 中,将此代码添加到以下函数中:
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 将保持方向。