iOS. How to enable and disable rotation on each UIViewController?

后端 未结 8 2117
栀梦
栀梦 2020-12-13 04:10

I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios

Example:

if flag {
   rotateDevice = false
}
e         


        
相关标签:
8条回答
  • 2020-12-13 04:41

    swift 5

    override var shouldAutorotate: Bool {
        false
    }
    
    0 讨论(0)
  • 2020-12-13 04:47

    An easy solution for the most common request (iPhone: portrait, iPad: all) in your AppDelegate:

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
    }
    
    0 讨论(0)
  • 2020-12-13 04:49

    swift 4

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        get {
            return .portrait
    
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:51

    I have the answer. On AppDelegate, if you rotate device, push viewcontroller, etc. This function always call

    Update for swift 3/4

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
    {
        return self.restrictRotation
    }
    

    self.restrictRotation is a custom parameter.

    How to use:

    In Appdelegate:

     var restrictRotation:UIInterfaceOrientationMask = .portrait
    

    In ViewController:

    When method ViewDidLoad or viewWillAppear is called. We will change like this:

    (UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all 
    

    and then this method on AppDelegate will be called.

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
    
    0 讨论(0)
  • 2020-12-13 04:56

    In swift 5, as from previous, if you want to allow (avoid others) a particular UIViewController to rotate in a certain orientation you have to override "supportedInterfaceOrientations" inside your UIViewController class like so:

    class MyViewController:UIViewController{
        override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
            get{
                return .portrait
            }
        }
    }
    

    Here there are the possible options:

    public struct UIInterfaceOrientationMask : OptionSet {
    
        public init(rawValue: UInt)
    
    
        public static var portrait: UIInterfaceOrientationMask { get }
    
        public static var landscapeLeft: UIInterfaceOrientationMask { get }
    
        public static var landscapeRight: UIInterfaceOrientationMask { get }
    
        public static var portraitUpsideDown: UIInterfaceOrientationMask { get }
    
        public static var landscape: UIInterfaceOrientationMask { get }
    
        public static var all: UIInterfaceOrientationMask { get }
    
        public static var allButUpsideDown: UIInterfaceOrientationMask { get }
    }
    

    Extended

    If you want to be able to distinguish between iPad or iPhone you could use UIUserInterfaceIdiom:

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        get{
            return UIDevice.current.userInterfaceIdiom == .phone ? [.portrait, . portraitUpsideDown]:.all //OBS -> You can also return an array
        }
    }
    

    where:

    public enum UIUserInterfaceIdiom : Int {
    
    
        case unspecified
    
        @available(iOS 3.2, *)
        case phone // iPhone and iPod touch style UI
    
        @available(iOS 3.2, *)
        case pad // iPad style UI
    
        @available(iOS 9.0, *)
        case tv // Apple TV style UI
    
        @available(iOS 9.0, *)
        case carPlay // CarPlay style UI
    }
    
    0 讨论(0)
  • 2020-12-13 04:59

    Found this to be by far the simplest implementation, and adaptable to use on specific view controllers. Add this to viewController class, after viewDidLoad()

    override open var shouldAutorotate: Bool {
            return false
        }
    
    0 讨论(0)
提交回复
热议问题