Home > Software design >  iOS 16 prevent UIViewController from rotation
iOS 16 prevent UIViewController from rotation

Time:10-05

I have an UIViewController that supports all UIInterfaceOrientationMasks But, in one certain case I need to prevent it from rotation Before iOS 16 i was just handling this case like this

override var shouldAutorotate: Bool {
    return !screenRecorderIsActive
}

And everything was working fine After update to iOS 16 my controller keeps rotating and I can't find a way to fix it

CodePudding user response:

Quoted from the iOS 16 release notes:

[UIViewController shouldAutorotate] has been deprecated is no longer supported. [UIViewController attemptRotationToDeviceOrientation] has been deprecated and replaced with [UIViewController setNeedsUpdateOfSupportedInterfaceOrientations].

Workaround: Apps relying on shouldAutorotate should reflect their preferences using the view controllers supportedInterfaceOrientations. If the supported orientations change, use `-[UIViewController setNeedsUpdateOfSupportedInterface

To implement dynamic supported interface orientations, you can use some code like this:

var screenRecorderIsActive: Bool {
    didSet {
        setNeedsUpdateOfSupportedInterfaceOrientations()
    }
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if screenRecorderIsActive {
        return [.landscape] // For example, or a variable representing the orientation when the condition was set
    }
    return [.all]
}
  • Related