簡體   English   中英

iOS-強制設備方向(如果視圖控制器以模態顯示)

[英]iOS - Force device orientation, if view controller is presented modally

ViewControllerA以模態形式顯示ViewControllerB。

ViewControllerA應為縱向。

ViewControllerB應該為LandscapeRight方向。

我這樣做: ViewControllerA

override func shouldAutorotate() -> Bool {
        return false
    }
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.Portrait
    }

ViewControllerB

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return UIInterfaceOrientation.LandscapeRight
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.LandscapeRight
    }

它是可行的,但是當我關閉ViewControllerB時,ViewControllerA也變為LandscapeRight。 如何解決? 謝謝。

解決方案

編輯AppDelegete文件。

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {

        if self.window?.rootViewController?.presentedViewController is ViewControllerB {

            let secondController = self.window!.rootViewController!.presentedViewController as! ViewControllerB

            if secondController.isPresented {
                return UIInterfaceOrientationMask.LandscapeRight;
            } else {
                return UIInterfaceOrientationMask.Portrait;
            }
        } else {
            return UIInterfaceOrientationMask.Portrait;
        }
    }

你需要允許ViewControllerA自轉,因為它需要返回到肖像模式ViewControllerB解雇后。 稍后它不會自動旋轉,因為在此屏幕上僅允許縱向方向。

解決方案是:

override func shouldAutorotate() -> Bool {
    return true
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM