【问题标题】:Set Orientation For Only One View Controller iOS 13 Swift仅为一个视图控制器 iOS 13 Swift 设置方向
【发布时间】:2020-01-22 03:48:46
【问题描述】:

我正在使用最新版本的 Swift 和 Xcode 创建一个仅限 iPhone 的应用程序,该应用程序中有 3 个 tabBar 部分,并且只有一个选项卡栏中我希望用户将其屏幕固定为横向。我尝试了我在 StackOverflow 和其他一些网站上找到的几乎所有可能的解决方案,但似乎都不起作用(包括这个:How to lock orientation of one view controller to portrait mode only in Swift)。有人知道为什么吗?

这是我要固定为横向的视图的视图控制器:


import UIKit
@objcMembers class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        }

        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        themes()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    //Some Code

///////////////////////////////////////

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

    override var shouldAutorotate: Bool {
        return false
    }
///////////////////////////////////////
}

【问题讨论】:

标签: ios swift


【解决方案1】:

您可以使用以下代码强制应用程序旋转方向,

UIDevice.current.setValue(.landscapeRight, forKey: "orientation")

当您希望应用程序旋转到横向模式时,请在 AppDelegate 中实现以下委托方法并返回横向掩码,

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask

【讨论】:

  • 它似乎不起作用,我相信是因为我的场景嵌入在 uitabbar 视图中
  • 您应该能够在点击预期选项卡时使用上述代码强制旋转应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 2020-01-24
  • 1970-01-01
相关资源
最近更新 更多