So in SwiftUI Apple has tried to force you to use just the Info.plist and Xcode General Deployment Info to force the view orientations. They don't support locking a SwiftUI view into a particular orientation which is ridiculous especially if your're using the camera. They also keep depcrecating orientation vars. So to get around these issues is complicated, but the code below works.
In AppDelegate have this.
static var orientationLock = UIInterfaceOrientationMask.all
static var deviceOrientation = UIDeviceOrientation.landscapeRight
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
Now you need a SwiftUI View Extension
extension View {
func detectOrientation(_ binding: Binding<UIDeviceOrientation>) -> some View {
self.modifier(OrientationDetector(orientation: binding))
}
}
struct OrientationDetector: ViewModifier {
@Binding var orientation: UIDeviceOrientation
let orientationMask = AppDelegate.orientationLock
func body(content: Content) -> some View {
content
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
if checkOrientation(orientationMask: orientationMask)
{
orientation = UIDevice.current.orientation
}
else
{
orientation = AppDelegate.deviceOrientation
}
}
}
}
Now it gets tricky since UIDevice.current.orientation has .faceUp and .faceDown which you don't care about so you need to ignore these view orientations, this is effectively what the checkOrienation() method does by saving the prior orientation when it does go into .faceUp or .faceDown.
func checkOrientation(orientationMask:UIInterfaceOrientationMask) -> Bool
{
//Here we're ignoring faceUp and faceDown and storing the prior orientation in AppDelegate.deviceOrientation
//This effectively eliminates these two problematic orientations so the App will ignore these and return the prior value
var status : Bool = false
switch UIDevice.current.orientation {
case .portrait :
if(orientationMask == .portrait || orientationMask == .portraitUpsideDown || orientationMask == .allButUpsideDown)
{
status = true
AppDelegate.deviceOrientation = UIDevice.current.orientation
}
case .landscapeLeft :
if orientationMask == .landscapeLeft || orientationMask == .allButUpsideDown
{
status = true
AppDelegate.deviceOrientation = UIDevice.current.orientation
}
case .landscapeRight :
if orientationMask == .landscapeRight || orientationMask == .allButUpsideDown
{
status = true
AppDelegate.deviceOrientation = UIDevice.current.orientation
}
case .portraitUpsideDown :
if orientationMask == .portraitUpsideDown || orientationMask == .allButUpsideDown
{
status = true
AppDelegate.deviceOrientation = UIDevice.current.orientation
}
case .faceUp :
status = false
case .faceDown :
status = false
case .unknown:
status = false
@unknown default:
status = false
}
return status
}
Now in your Primary SwiftUI Views you need an init and .detectOrienation($orientation)
@State private var orientation = UIDevice.current.orientation
init() {
//Lock into all view rotations
AppDelegate.orientationLock = .allButUpsideDown
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .allButUpsideDown))
UIApplication.navigationTopViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
//At bottom of SwiftUI view add this :
.detectOrientation($orientation)
What the above does for the SwiftUI View is let it rotate for .allButUpsideDown view rotations. If you want to lock it, set .portrait, or .landscapeLeft or .landscapeRight.
The Primary SwiftUI View will declare the @State var orientation, and all your SwiftUI SubViews that need orientation need to consume this same var.
Thus a SwiftUI Subview needs this :
@Binding private var orientation:UIDeviceOrientation
init(orientation:Binding<UIDeviceOrientation>)
self._orientation = orientation
}
Now in your Primary and SubViews you do this to handle the layout in a particular view orientation.
if orientation.isLandscape {
//SwiftUI elements for Landscape
} else {
//SwiftUI elements for Portrait
}
And thats it, it's a lot of code to get around this problem, but it works in SwiftUI version 5 and iOS 16+ and Apple will approve it for an App Release also.
We really need to lobby Apple to support natively locking a SwiftUI View into a particular view orientation, this can be done by just calling a simple SwiftUI method. They also need to create a new var for UIDeviceOrientation that ignores .faceUp and .faceDown so you can consume this var in your SWiftUI view and layout your view elements accordingly.