2016-05-15 155 views
5

我是Swift中的新手,並且在viewController中將方向鎖定爲縱向。其實我已經在我的自定義導航控制器swift中的viewController的鎖定方向

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { 
    if (self.visibleViewController is ViewController) 
    { 
     return UIInterfaceOrientationMask.Portrait 
    } 
    return .All 
} 

一切使用此代碼鎖定它工作正常和視圖控制器被鎖定到portrait.The問題是,當返回到該控制器從另一個在橫向模式。 如果我在景觀中返回到ViewController(從NextViewController後退),那麼ViewController出現在橫向上。任何建議?

+0

http://stackoverflow.com/a/20987296/1959140 在視圖做apprear,改變方向。 –

回答

0

您可以覆蓋以下

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

方法然後,在您viewDidLoad中,通過添加以下

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") 
+0

感謝您的回覆。缺少的代碼是UIDevice.currentDevice()。setValue(UIInterfaceOrientation.Portrait.rawValue,forKey:「orientation」),但它在viewWillAppear上效果更好。 –

0

代碼強制定向。swift3.0

爲了限制不同的方向針對不同意見您需要做以下事情:

我ñ應用程序的委託文件:

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

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

      return UIInterfaceOrientationMask.All; 

     } else { 
      return UIInterfaceOrientationMask.Portrait; 
     } 

    } 
5

在迅速3此解決方案有效

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { 
    if self.window?.rootViewController?.presentedViewController is LockedViewController { 
     return UIInterfaceOrientationMask.portrait 
    } else { 
     return UIInterfaceOrientationMask.all 
    } 
} 

更改LockedViewController你想鎖定的控制器相匹配。

+0

它適合我。非常感謝你:) –

+0

哇。這正是我正在尋找的。非常感謝你張貼這個! – Nico