代码之家  ›  专栏  ›  技术社区  ›  Nguyen Hoan

从其他屏幕返回时,屏幕从纵向旋转到lanscape

  •  0
  • Nguyen Hoan  · 技术社区  · 5 年前

    我有两个视图控制器(A&B)。

    A为自动旋转,B为强制横向旋转。

    当用户关闭纵向方向锁定,它的工作正常:总是旋转跟随设备方向。

    但当“纵向方向锁定”处于启用状态时,从B返回时A的方向始终是横向的。

    谢谢。

    0 回复  |  直到 5 年前
        1
  •  0
  •   JamesVoo    5 年前

    这里- https://stackoverflow.com/a/41811798/10261915 你有答案

    struct AppUtility {
    
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
    
            if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = orientation
            }
        }
    
        /// OPTIONAL Added method to adjust lock and rotate to the desired orientation
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
    
            self.lockOrientation(orientation)
    
            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
            UINavigationController.attemptRotationToDeviceOrientation()
        }
    
    }
    

    AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
    
        2
  •  0
  •   bacat26    5 年前

    添加到BViewController:

    override func viewDidLoad() {
            super.viewDidLoad()
       UIDevice.current.setValue(Int(UIInterfaceOrientation.landscapeLeft.rawValue), forKey: "orientation")
        }
    
        override func viewWillDisappear(_ animated: Bool) {
            UIDevice.current.setValue(Int(UIInterfaceOrientation.portrait.rawValue), forKey: "orientation")
        }