如何在SWIFT中将单视图控制器的方向锁定为纵向模式

如何在SWIFT中将单视图控制器的方向锁定为纵向模式

因为我的应用程序得到了所有方向的支持。我只想锁定特定UIViewController的纵向模式。

(例如,假设它是选项卡式应用程序,并且当signin View以模态方式出现时,我只希望该signin View只有在用户如何旋转设备或当前设备方向的情况下才能进入纵向模式)



天涯尽头无女友
浏览 399回答 3
3回答

胡说叔叔

当您有一个复杂的视图层次结构时,事情会变得非常混乱,比如有多个导航控制器和/或选项卡视图控制器。这个实现让各个视图控制器在希望锁定方向时设置它,而不是依赖AppDelegate通过迭代子视图来找到它们。SWIFT 3&4在附录中:/// set orientations you want to be allowed in this property by defaultvar orientationLock = UIInterfaceOrientationMask.allfunc a pplication(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {         return self.orientationLock}在其他一些全局struct或Helper类中,我在这里创建了AppUtility:struct AppUtility {     static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {         if let delegate = UIApplication.shared.delegate as? AppDelegate {             delegate.orientationLock = orientation        }     }     /// OPTIONAL Added method to adjust lock and rotate to the desired orientation         static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {         self.lockOrientation(orientation)         UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")         UINavigationController.attemptRotationToDeviceOrientation()     }}然后,在所需的ViewController中,您要锁定方向: override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)     AppUtility.lockOrientation(.portrait)     // Or to rotate and lock    // AppUtility.lockOrientation(.portrait, andRotateTo: .     portrait)}override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)     // Don't forget to reset when view is being removed    AppUtility.lockOrientation(.all)}如果iPad或通用应用程序确保在目标设置->General->DeploymentInfo中签入“Required全屏”。supportedInterfaceOrientationsFor如果不选中委托,则不会调用该委托。

SMILET

SWIFT 4var orientationLock = UIInterfaceOrientationMask.allfunc application(_ application: UIApplication, supportedInterfaceOrientationsFor window:  UIWindow?) -> UIInterfaceOrientationMask {     return self.orientationLock}struct AppUtility {     static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {         if let delegate = UIApplication.shared.delegate as? AppDelegate {             delegate.orientationLock = orientation        }     }     static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {         self.lockOrientation(orientation)         UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")     }}您的视图控制器如果您只需要纵向方向,请添加以下行。您必须将此应用于所有ViewController需要显示的纵向模式。override func viewWillAppear(_ animated: Bool) {AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo:  UIInterfaceOrientation.portrait)     }这将使屏幕定位为其他视图控制器根据设备的物理方向。override func viewWillDisappear(_ animated: Bool) {         AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all)     }

弑天下

添加此代码以强制显示并锁定它:override func viewDidLoad() {     super.viewDidLoad()     // Force the device in portrait mode when the view controller gets loaded    UIDevice.currentDevice().     setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation") }override func shouldAutorotate() -> Bool {     // Lock autorotate    return false}override func supportedInterfaceOrientations() -> Int {     // Only allow Portrait    return Int(UIInterfaceOrientationMask.Portrait.rawValue)}     override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {     // Only allow Portrait    return UIInterfaceOrientation.Portrait}在AppDelegate-set Support InterfaceOrientationsForWindow中,您希望整个应用程序支持任何方向:func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {     return UIInterfaceOrientationMask.All}
打开App,查看更多内容
随时随地看视频慕课网APP