旋转显示的视图控制器但不显示视图控制器

时间:2018-04-14 21:24:30

标签: ios swift

我希望能够旋转模态呈现的视图控制器,但不能旋转呈现它的视图控制器。我正在控制我的应用中的方向更改,如下所示:

在我的AppDelegate

var orientationLock: UIInterfaceOrientationMask = .portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return orientationLock
}

在我的OrientationManager

struct OrientationManager {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation
        }
    }

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation: UIInterfaceOrientation) {
        self.lockOrientation(orientation)
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }
}

在我的ModallyPresentedViewController

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    OrientationManager.lockOrientation(.all)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    OrientationManager.lockOrientation(.portrait, andRotateTo: .portrait)
}

目前这有效,但它会旋转下面的模态视图,这就是为什么我必须在视图消失时调用OrientationManager.lockOrientation(.portrait, andRotateTo: .portrait)。当视图消失时,呈现视图控制器仍然会旋转几秒钟,然后再返回到纵向模式。如何防止呈现视图控制器旋转?谢谢!

(旁注,有人知道UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")是否是私有API?)

0 个答案:

没有答案