0

I know this setting in XCode

enter image description here

It works well if I have to lock my entire app in one orientation

but what if I want to lock only particular view (say camera view) into landscapeRight then what should be done?

I have tried following approach

class AppDelegate: NSObject, UIApplicationDelegate {
    
    static var orientationLock = UIInterfaceOrientationMask.portrait {
        didSet {
            if #available(iOS 16.0, *) {
                UIApplication.shared.connectedScenes.forEach { scene in
                    if let windowScene = scene as? UIWindowScene {
                        windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: orientationLock))
                    }
                }
            } else {
                if orientationLock == .landscapeRight {
                    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
                } else {
                    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
                }
            }
        }
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        return true
    }
    
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return AppDelegate.orientationLock
    }
}  

View

import SwiftUI

    struct CameraPage: View {
        var body: some View {
            ZStack {
                Color.black
                HStack {
                    Spacer()
                
                    Button {
                        
                    } label: {
                        Text("Capture")
                    }
                    .frame(width: 80, height: 80)
                    .background(Color.white)
                    .clipShape(Circle())
                }.padding(.trailing)
            }.onAppear {
                AppDelegate.orientationLock = .landscapeRight
            }
        }
    }

It changes the orientation to landscapeRight but do not lock the orientation if I hold my phone in potrait mode it again switches the camera view to the potrait mode why?

is it possible lock any view in desired orientation while using Swift UI?

Sanket Bhangale
  • 135
  • 1
  • 7
  • Does this answer your question? [SwiftUI: How do I lock a particular View in Portrait mode whilst allowing others to change orientation?](https://stackoverflow.com/questions/66037782/swiftui-how-do-i-lock-a-particular-view-in-portrait-mode-whilst-allowing-others) – lazarevzubov Jun 02 '23 at 05:04
  • More similar questions: https://stackoverflow.com/questions/67035307/swiftui-how-to-force-landscape-without-appdelegate, https://stackoverflow.com/questions/70224435/swiftui-5-how-to-lock-orientation-for-a-specific-view. – lazarevzubov Jun 02 '23 at 05:06
  • Thank you @lazarevzubov I will check these questions and get back – Sanket Bhangale Jun 02 '23 at 05:09

0 Answers0