Unable to force UIViewController orientation

后端 未结 3 743
爱一瞬间的悲伤
爱一瞬间的悲伤 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:46

    try this for force to LandscapeRight mode only

    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
    
            if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
            {
                let value = UIInterfaceOrientation.LandscapeRight.rawValue
                UIDevice.currentDevice().setValue(value, forKey: "orientation")
            }
    
        }
    

    and then use a category like this

        import UIKit
    
    extension UINavigationController{
    
        override public func shouldAutorotate() -> Bool
        {
        return (self.viewControllers.last?.shouldAutorotate())!
        }
    
        override public func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
        {
        return (self.viewControllers.last?.supportedInterfaceOrientations())!;
        }
    
        override public func preferredInterfaceOrientationForPresentation()-> UIInterfaceOrientation
        {
        return (self.viewControllers.last?.preferredInterfaceOrientationForPresentation())!;
        }
    
    }
    

    EDITED

    If you are not using navigation controller use this

    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
    
            if(self.supportedInterfaceOrientations() == UIInterfaceOrientationMask.LandscapeRight && UIDevice.currentDevice().orientation != UIDeviceOrientation.LandscapeRight)
            {
                let value = UIInterfaceOrientation.LandscapeRight.rawValue
                UIDevice.currentDevice().setValue(value, forKey: "orientation")
            }
    
        }
    
        override func supportedInterfaceOrientations() ->UIInterfaceOrientationMask
        {
            return .LandscapeRight;
        }
    

    I hope this helps you

    0 讨论(0)
  • 2020-12-03 20:56

    probably I´m crazy ;-), but why don´t u use this?

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题