首页 > 解决方案 > 将屏幕从纵向转换为横向它在从左到右旋转时不会保持横向,反之亦然

问题描述

我确实根据屏幕要求更改了 uidevice 的方向。

例如屏幕A,我只需要制作肖像。

屏幕 B 它应该是横向的,我做到了。

但是在返回屏幕 A 并再次进入屏幕 B 并将设备从左向右或从右向左旋转后,屏幕 B 的方向变为纵向。

如果有人对此有解决方案,请发表评论。

在这里,我将解释我在项目中所做的整个场景:

AppDelegate.swift

var orientationLock = UIInterfaceOrientationMask.portrait   //Defined variable for orientation

func application(_ application: UIApplication,
                 supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .allButUpsideDown : .portrait
}

在 A 类

   override func viewWillAppear(_ animated: Bool) {
       super.viewWillAppear(animated)
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        Global.appDelegate.shouldRotate = false
    }

在 B 类

DispatchQueue.main.async {
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}
Global.appDelegate.shouldRotate = false

现在我尝试在所有旋转中制作 B 屏幕横向,为此我尝试了以下方法:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate(alongsideTransition: nil, completion: {
        _ in
        if UIDevice.current.orientation == .portrait || UIDevice.current.orientation == .portraitUpsideDown {
            //                Global.AppUtility.lockOrientation(.landscape)
            let value = UIInterfaceOrientation.landscapeLeft.rawValue
            UIDevice.current.setValue(value, forKey: "orientation")
            Global.appDelegate.shouldRotate = true
        }
    })

}

但是这种方法只在第一次起作用,在选择 A 并再次进入屏幕 B 后,它保持纵向。

标签: iosswift3swift4landscape-portrait

解决方案


推荐阅读