【问题标题】:Swift 3 - Force autorotationSwift 3 - 强制自转
【发布时间】:2016-12-20 00:26:53
【问题描述】:

我正在使用this 代码。

例如,我有 view1(仅纵向)和 view2(纵向+横向)。当您在 view1 中并单击一个按钮时,您会在整个屏幕上打开 view2 并弹出窗口。当您关闭 view2 并且 view1 变得可见时,如果视图 2 处于横向状态,我想自动将其转换为纵向模式。有什么建议吗?

【问题讨论】:

标签: ios swift autorotate


【解决方案1】:

正如您所说,您使用的是弹出窗口,我确定您使用的是导航控制器。这是视图层次结构。

有三个viewControllers对应那些xib。

viewController 仅设置为portrait,第二个ViewController 设置为portrait and landScapeLeft(您可以将其更改为您需要的任何内容)。它可以按照您的要求正常工作。当第二个是横向时,弹出到第一个,它将被强制设置为纵向。

NavViewController

ViewController

SecondViewController

//NavViewController.swift

class NavViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override var shouldAutorotate: Bool {
        return (visibleViewController?.shouldAutorotate)!
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return (visibleViewController?.supportedInterfaceOrientations)!
    }
}

//ViewController.swift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let value =  UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        UIViewController.attemptRotationToDeviceOrientation()
    }
}

SecondViewController.swift 类 SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return [.portrait, .landscapeLeft]
    }
}

【讨论】:

  • 我只需要`let value = UIInterfaceOrientation.portrait.rawValue UIDevice.current.setValue(value, forKey: "orientation") UIViewController.attemptRotationToDeviceOrientation()` 谢谢:)
  • Bogdan Bogdanov 我也是:p
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多