I've been force locking the screen orientation using this, which works fine on iPhone simulators:
@main
struct MainApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
static var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
}
And used on a view like so:
struct ContentView : View {
var body : some View {
ZStack {
Text("Hello World!")
.onAppear{
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
AppDelegate.orientationLock = .portrait
}.onDisappear{
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
AppDelegate.orientationLock = .portrait
}
}
}
}
However, this doesn't work on iPad simulators. It is not enough to deselect orientations in target's Deployment Info because some views have to be different orientations. Any advice would be greatly appreciated!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.