如何仅在 Swift 中将一个视图控制器的方向锁定为纵向模式 [英] How to lock orientation of one view controller to portrait mode only in Swift

查看:38
本文介绍了如何仅在 Swift 中将一个视图控制器的方向锁定为纵向模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我的应用支持所有方向.我只想将纵向模式锁定到特定的 UIViewController.

Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController.

例如假设它是选项卡式应用程序,并且当登录视图以模态显示时,我只希望登录视图处于纵向模式,无论用户如何旋转设备或当前设备方向如何

e.g. assume it was Tabbed Application and when SignIn View appear modally, I only want that SignIn View to the portrait mode only no matter how the user rotate the device or how the current device orientation will be

推荐答案

当你有一个复杂的视图层次结构时,事情会变得非常混乱,比如有多个导航控制器和/或选项卡视图控制器.

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers.

此实现将它放在各个视图控制器上以设置它们何时想要锁定方向,而不是依赖 App Delegate 通过迭代子视图来找到它们.

This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews.

斯威夫特 3、4、5

在 AppDelegate 中:

In AppDelegate:

/// set orientations you want to be allowed in this property by default
var orientationLock = UIInterfaceOrientationMask.all

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return self.orientationLock
}

在其他一些全局结构或辅助类中,我在这里创建了 AppUtility:

In some other global struct or helper class, here I created 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 中:

Then in the desired ViewController you want to lock orientations:

 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 或通用应用

确保需要全屏"在目标设置中检查 ->一般 ->部署信息.supportedInterfaceOrientationsFor 如果未选中,则不会调用委托.

Make sure that "Requires full screen" is checked in Target Settings -> General -> Deployment Info. supportedInterfaceOrientationsFor delegate will not get called if that is not checked.

这篇关于如何仅在 Swift 中将一个视图控制器的方向锁定为纵向模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆