swift中的viewController的锁定方向

问题描述:

我是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://*.com/a/20987296/1959140 在视图做apprear,改变方向。 –

您可以覆盖以下

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上效果更好。 –

代码强制定向。swift3.0

为了限制不同的方向针对不同意见您需要做以下事情:

我ñ应用程序的委托文件:

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

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

      return UIInterfaceOrientationMask.All; 

     } else { 
      return UIInterfaceOrientationMask.Portrait; 
     } 

    } 

在迅速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