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
in iOS16+ maybe you can try to call
setNeedsUpdateOfSupportedInterfaceOrientations
of UIViewController.in addition, if you use
NavigationController
orUITabBarController
, you also should setshouldAutorotate
andsupportedInterfaceOrientations
as yourUIViewController
.You should set your
shouldAutorotate
totrue
as autorotation only happens into supported interface orientations, setting it tofalse
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 callingsetNeedsUpdateOfSupportedInterfaceOrientations()
. If this one doesn’t help try making sure that your VC is connected by parent/child relationship with your window’srootViewController
and none of parent VCs is not taking interface rotation control away from your VC.