当您具有复杂的视图层次结构时,如拥有多个导航控制器和/或制表视图控制器,事情会变得相当混乱.(e.g. Assume it was Tabbed Application and when SignIn VIEw appear modally,I only want that SignIn VIEw to the portrait mode only no
matter how the user rotate the device or how the current device
orIEntation will be)
该实现将它放在个人视图控制器上,以便设置何时锁定方向,而不是依赖App代理通过遍历子视图来查找它们.
Swift 3
在AppDelegate中:
/// set orIEntations you want to be allowed in this property by defaultvar orIEntationLock = UIInterfaceOrIEntationMask.allfunc application(_ application: UIApplication,supportedInterfaceOrIEntationsFor window: UIWindow?) -> UIInterfaceOrIEntationMask { return self.orIEntationLock}
在其他一些全局结构或帮助类中,我创建了AppUtility:
struct AppUtility { static func lockOrIEntation(_ orIEntation: UIInterfaceOrIEntationMask) { if let delegate = UIApplication.shared.delegate as? AppDelegate { delegate.orIEntationLock = orIEntation } } /// OPTIONAL Added method to adjust lock and rotate to the desired orIEntation static func lockOrIEntation(_ orIEntation: UIInterfaceOrIEntationMask,andRotateto rotateOrIEntation:UIInterfaceOrIEntation) { self.lockOrIEntation(orIEntation) UIDevice.current.setValue(rotateOrIEntation.rawValue,forKey: "orIEntation") }}
然后在所需的VIEwController中锁定方向:
overrIDe func vIEwDIDAppear(_ animated: Bool) { super.vIEwDIDAppear(animated) AppUtility.lockOrIEntation(.portrait) // Or to rotate and lock // AppUtility.lockOrIEntation(.portrait,andRotateto: .portrait)}overrIDe func vIEwWilldisappear(_ animated: Bool) { super.vIEwWilldisappear(animated) // Don't forget to reset when vIEw is being removed AppUtility.lockOrIEntation(.all)}总结
以上是内存溢出为你收集整理的只有在Swift中,如何将一个视图控制器的方向锁定到纵向模式全部内容,希望文章能够帮你解决只有在Swift中,如何将一个视图控制器的方向锁定到纵向模式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)