【问题标题】:Landscape application only one controller in Portrait - Swift 3横向应用程序只有一个纵向控制器 - Swift 3
【发布时间】:2017-03-07 12:09:41
【问题描述】:

在应用代理中:

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

在我的视图控制器(MainViewController)中我添加了

override func viewDidLoad() {
    super.viewDidLoad()

    let value = UIInterfaceOrientation.portrait.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    supportedInterfaceOrientations()
    preferredInterfaceOrientationForPresentation()
    // Do any additional setup after loading the view.
}

private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue)
}

private func shouldAutorotate() -> Bool {
    return true
}

private func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {

    // Only allow Portrait
    return UIInterfaceOrientation.portrait
}

这是我想要在纵向模式下工作的应用程序中唯一的控制器。除了横向模式下的一切。

但是我尝试了很多东西仍然无法理解为什么它不起作用。

提前致谢。对不起,我是 swift 的菜鸟。

【问题讨论】:

    标签: uiviewcontroller swift3 orientation landscape-portrait


    【解决方案1】:

    在 appdelegate 中编写这段代码

    var shouldRotate = false
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if shouldRotate {
            return .landscape
        }
        else {
            return .portrait
        }
    }
    

    在 viewDidload() 中将此代码设置到您的视图控制器

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.shouldRotate = true // or false to disable rotation
        //you can manage only changing true or false 
    

    希望对您有所帮助

    【讨论】:

    • 嘿@seggy 不,它不起作用。它确实将键盘的布局更改为纵向。但是视图控制器仍然是横向的。
    • dude 它在我的项目中正常工作。我觉得你有什么问题。
    • 嘿@seggy 当我旋转设备并且不强制改变方向时它可以工作。我希望控制器强制改变方向
    • 你可以把 .landscape 改成 .all
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2011-05-12
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多