how to lock portrait orientation for only main view using swift

后端 未结 16 1864
野性不改
野性不改 2020-12-07 13:38

I have created an application for iPhone, using swift, that is composed from many views embedded in a navigation controller. I would like to lock the main v

16条回答
  •  無奈伤痛
    2020-12-07 14:08

    Same JasonJasonJason answer in Swift 4.2+ (It worked correctly with iOS 11)

    1- Override shouldAutorotate and supportedInterfaceOrientations as shown below.

    extension UINavigationController {
    
    open override var shouldAutorotate: Bool {
        return true
    }
    
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return (visibleViewController?.supportedInterfaceOrientations)!
        }
    }
    

    2- And your main viewcontroller (portrait at all times), should have:

     public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    

    3- Then, in your subviewcontrollers that you want to support portrait or landscape:

     public override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.all
    }
    

提交回复
热议问题