I know this setting in XCode
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?