IOS How to enable and disable rotation on each UIViewController?

I have a UIViewController, I want to disable or enable screen rotation in different scenarios

Example:

if flag {
   rotateDevice = false
}
else {
   rotateDevice = true
}

How can i do this?

+21
source share
5 answers

I have an answer. In AppDelegate, if you rotate the device, click on the viewcontroller, etc. This function always calls

Update for Swift 3/4

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

self.restrictRotation is a user parameter.

How to use:

In Appdelegate:

 var restrictRotation:UIInterfaceOrientationMask = .portrait

In ViewController:

When the ViewDidLoad or viewWillAppear method 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
+39
source

shouldAutorotate supportedInterfaceOrientations UIViewController. . :

override func shouldAutorotate() -> Bool {
    return true
}

, . :

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape
}

: , - :

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if myFlag {
        return .Landscape
    } else {
        return .All
    }
}

( myFlag , Landscape . , ).

+21

4

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait

    }
}
+13

In swift 5, as in the previous case, if you want to allow (avoid others) the rotation of a specific UIViewController in a specific orientation, you must override "supportInterfaceOrientations" inside your UIViewController class as follows:

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

There are 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 distinguish between iPad or iPhone, you can 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
}
+3
source

set device orientation in Target → General → Deployment Info → Portrait

enter image description here

-3
source

Source: https://habr.com/ru/post/1651379/


All Articles