Unable to force UIViewController orientation

后端 未结 3 753
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 20:21

I have a portrait application with one landscape ViewController.

I\'ve been digging a lot on how to force the orientation to landscape when the app is locked to port

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 20:57

    As an update to Reinier Melian's post, here is the UINavigationController extension in Swift 3:

    import UIKit
    
    extension UINavigationController {
        override open var shouldAutorotate: Bool {
            return (self.viewControllers.last?.shouldAutorotate)!
        }
    
        override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return (self.viewControllers.last?.supportedInterfaceOrientations)!
        }
    
        override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation)!
        }
    }
    

    Unfortunately, this code crashes if you call a UIImagePickerController, as that is contained within a UINavigationController whose last view controller is nil.

提交回复
热议问题