skip to Main Content

How to lock app orientation to a specific mode in Xcode 13?

2

Answers


  1. Chosen as BEST ANSWER

    1. Lock orientation to a specific mode i.e Portrait

    In your AppDelegate, take a variable as

    var restrictRotation:UIInterfaceOrientationMask = .portrait
    

    and paste this override function

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

    2. Lock orientation for a UIViewController

    In your AppDelegate, take a variable as

    var restrictRotation:UIInterfaceOrientationMask = .all
    

    and paste this override function

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

    And very importantly call the below line from viewWillAppear or viewDidLoad method

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

  2. enter image description here

    Here’s a screenshot of Xcode. This UI is where you can define it globally, or you can do it programmatically, as already someone else has answered.

    Additionally, you can configure the iPad here in Target -> build settings
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search