This works for my view, in Xcode 13
The only problem is the upside down portrait mode view
struct ContentView5: View {
var body: some View {
ZStack {
(
Text("Hello, World!")
)
.onAppear() {
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
.onDisappear() {
var value = UIInterfaceOrientation.portrait.rawValue
if UIDevice.current.orientation.isPortrait
{
UIDevice.current.setValue(value, forKey: "orientation")
value = UIInterfaceOrientation.unknown.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
else
{
value = UIInterfaceOrientation.unknown.rawValue
}
}
}
}
}
Doc:
https://developer.apple.com/documentation/uikit/uiinterfaceorientation