skip to Main Content

I want to lock orientation to landscape right and landscape left only in one view controller. I use this code in my view controller to do it:

override var shouldAutorotate: Bool {
    return false
}
    
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
}

But it doesn’t work for me if I rotate device. What I do wrong?

2

Answers


  1. in iOS16+ maybe you can try to call setNeedsUpdateOfSupportedInterfaceOrientations of UIViewController.

    in addition, if you use NavigationController or UITabBarController, you also should set shouldAutorotate and supportedInterfaceOrientations as your UIViewController.

    Login or Signup to reply.
  2. You should set your shouldAutorotate to true as autorotation only happens into supported interface orientations, setting it to false will prevent your view controller from rotating to the landscape. Also cascades of parent view controllers may block your controller from the rotation – set breakpoints and make sure that those methods are being called. You also can force the orientation update by calling setNeedsUpdateOfSupportedInterfaceOrientations(). If this one doesn’t help try making sure that your VC is connected by parent/child relationship with your window’s rootViewController and none of parent VCs is not taking interface rotation control away from your VC.

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