Post

Replies

Boosts

Views

Activity

Reply to How to lock screen orientation for a specific view?
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()     })
Aug ’20
Reply to SwiftUI: Force orientation on a per screen basis
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()         })
Aug ’20