struct NetflixApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
}
class AppDelegate: NSObject {
// 屏幕旋转锁定
static var orientationLock = UIInterfaceOrientationMask.portrait
}
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
}
SomeView
.onDisappear(perform: {
DispatchQueue.main.async {
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
})
.onAppear(perform: {
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
})
Post
Replies
Boosts
Views
Activity
This is not a perfect solution.
In IOS 14. My solution is as follows:
struct NetflixApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
}
class AppDelegate: NSObject {
// 屏幕旋转锁定
static var orientationLock = UIInterfaceOrientationMask.portrait
}
extension AppDelegate: UIApplicationDelegate {
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return AppDelegate.orientationLock
}
}
SomeView
.onDisappear(perform: {
DispatchQueue.main.async {
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}
})
.onAppear(perform: {
AppDelegate.orientationLock = UIInterfaceOrientationMask.landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
})