为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

天涯尽头无女友 2019-10-16 12:09:08
如何在SWIFT中将单视图控制器的方向锁定为纵向模式因为我的应用程序得到了所有方向的支持。我只想锁定特定UIViewController的纵向模式。(例如,假设它是选项卡式应用程序,并且当signin View以模态方式出现时,我只希望该signin View只有在用户如何旋转设备或当前设备方向的情况下才能进入纵向模式)
查看完整描述

3 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

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

这个实现让各个视图控制器在希望锁定方向时设置它,而不是依赖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如果不选中委托,则不会调用该委托。



查看完整回答
反对 回复 2019-10-17
?
SMILET

TA贡献1796条经验 获得超4个赞

SWIFT 4

var 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)

    }



查看完整回答
反对 回复 2019-10-17
?
弑天下

TA贡献1818条经验 获得超7个赞

添加此代码以强制显示并锁定它:

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}



查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 392 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信