To offer a slight change to Jonathan's great solution. This version answers folks above asking about how to use other orientations besides just "landscapeLeft".
First, Jonathan stated "We set the project orientation to only support Portrait mode." Don't do this if you want to allow other orientations. So for the project leave "Portrait", "Landscape Left" and "Landscape Right" checked along with "Upside Down" if you also wish this orientation.
Second, in the onAppear
of the view you wish to ALLOW rotation, set the following:
AppDelegate.orientationLock = UIInterfaceOrientationMask.allButUpsideDown
The above replaces .landscapeLeft
from the original example. Note, if you want to allow upside down, make it .all
instead.
That's it. Everything else is as stated by Jonathan and others. The result is that the app will be portrait locked except for the views that you update with onAppear
and the code above. For those the user will have access to all the orientations (or all but upside down).
BUG IN CLIENT OF UIKIT: Setting UIDevice.orientation is not supported. Please use UIWindowScene.requestGeometryUpdate(_:)
The solution can be find iOS 16 Scene orientation issue – Jube