代码之家  ›  专栏  ›  技术社区  ›  Raj

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

  •  0
  • Raj  · 技术社区  · 7 年前

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

    问题是当图形视图在活动屏幕上,应用程序进入后台时。
    当应用程序返回活动状态时,图形的方向将更改为纵向模式,这是不需要的。
    有没有办法保持图形视图的方向?
    注意:图形视图控制器未与任何导航控制器连接,在按钮单击事件后以编程方式调用。

    提前谢谢。

    2 回复  |  直到 7 年前
        1
  •  0
  •   Tung Fam    7 年前

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

    我找到这个答案是为了让事情变得更简单,而不是在这里再写一次。

    解决方案: link .

    希望有帮助!

        2
  •  0
  •   Raj    7 年前

    以下是一些先决条件。

    第一 我们为应用程序选择所需的方向:

    enter image description here

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

    enter image description here



    现在进入编码部分: 在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 将保留方向。