ios - 单 View Controller swift 5 中的方向支持

标签 ios swift

我需要在单 View Controller 中提供方向支持

我尝试过,但没有成功。现在整个应用程序的方向。

enter image description here

override func viewDidLoad() {
    super.viewDidLoad()

    self.lockOrientation()
}

func lockOrientation() {
    let orientationValue = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(orientationValue, forKey: "orientation")
}

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}

最佳答案

将以下代码放入AppDelegate

仅在项目常规设置中将设备方向保留为纵向

var restrictRotation: Bool = true

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if restrictRotation {
        return .portrait
    } else {
        return .all
    }
}

在下面输入您想要允许/禁用方向的代码。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.restrictRotation(false) //TRUE TO RESTRICT ORIENTATION && FALSE TO ALLOW ORIENTATION
}

func restrictRotation(_ restriction: Bool) {
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.restrictRotation = restriction
}

关于ios - 单 View Controller swift 5 中的方向支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56421288/

相关文章:

ios - NSComparator block 中的后台发生了什么?

快速数字格式化

swift - 如何使用prepare for segue 将图像从表格单元格传输到新的 View Controller ?

ios - 将 NSDate 和 double 保存到实体 (CoreData)

使用 Facebook 的 iOS 应用程序——被拒绝

ios - 提交 iPhone 应用程序时用于 SKU # 的良好约定

ios - Parse.com回调存在吗?

ios - 自定义 UIToolBar 的正确方法是什么?

swift - Buildtime Canvas Pod xCode 问题 |缺少 [super awakeFromNib] 调用 ||来自 CSAnimationView.m 文件

swift - 无重叠的随机节点位置