The answer by Kush Bhavsar almost worked for me, but it failed to revert back to the previous orientations when the view disappeared. Here's a solution that does revert back to the previous orientations as expected (and I improved the naming and organization of some things to be clearer).
Create View extension supportedInterfaceOrientations:
extension View { @ViewBuilder func supportedInterfaceOrientations( _ orientations: UIInterfaceOrientationMask ) -> some View { modifier(SupportedInterfaceOrientationsModifier(orientations: orientations)) } } private struct SupportedInterfaceOrientationsModifier: ViewModifier { let orientations: UIInterfaceOrientationMask @State private var previousOrientations = UIInterfaceOrientationMask.portrait func body(content: Content) -> some View { content .onAppear() { previousOrientations = AppDelegate.supportedInterfaceOrientations AppDelegate.supportedInterfaceOrientations = orientations } .onDisappear() { AppDelegate.supportedInterfaceOrientations = previousOrientations } } }
In AppDelegate, add static var:
static var supportedInterfaceOrientations = UIInterfaceOrientationMask.portrait { didSet { updateSupportedInterfaceOrientationsInUI() } }
And add AppDelegate extension:
extension AppDelegate { private static func updateSupportedInterfaceOrientationsInUI() { if #available(iOS 16.0, *) { UIApplication.shared.connectedScenes.forEach { scene in if let windowScene = scene as? UIWindowScene { windowScene.requestGeometryUpdate( .iOS(interfaceOrientations: supportedInterfaceOrientations) ) } } UIViewController.attemptRotationToDeviceOrientation() } else { if supportedInterfaceOrientations == .landscape { UIDevice.current.setValue( UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation" ) } else { UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation") } } } func application( _ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow? ) -> UIInterfaceOrientationMask { Self.supportedInterfaceOrientations } }