【问题标题】:if app device orientation is portrait how to set force landscape only for one view constroller in ios, swift 2.3如果应用程序设备方向是纵向的,如何为 ios 中的一个视图控制器设置强制横向,swift 2.3
【发布时间】:2017-03-07 13:24:55
【问题描述】:

对于我的项目,设备方向是portrair。但仅适用于view did load 时的一个视图控制器,没有rotating 电话,我想像“向左”一样旋转视图。我试过以下。在我看来确实加载了,

override func viewDidLoad() {
        super.viewDidLoad()

        let value = UIInterfaceOrientation.LandscapeLeft.rawValue
        UIDevice.currentDevice().setValue(value, forKey: "orientation")

    }

我用了这个方法……

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

但什么也没发生。然后我也使用下面的方法和上面的方法。

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.All
    }

然后它会导致崩溃。没有什么对我有用。任何想法如何做到这一点。

这是我的项目设备方向设置看起来像...

我该怎么做。希望您对此有所帮助。

【问题讨论】:

  • AppDelegate 中尝试func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask 然后检查您的控制器是否可见

标签: ios swift uiinterfaceorientation


【解决方案1】:

做你想做的最好的方法是在你的应用设置中allow all Orientations

然后将 "should autorotate" 变量设置为 true il 你所有的 viewControllers

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

然后根据您的 ViewContoller

设置 supportedInterfaceOrientations
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .Landscape

}

override func shouldAutorotate() -> Bool {
    return false
}

例如这里我有两个视图控制器,当调用函数 viewDidLoad 时视图方向会改变。

import UIKit

class PortraitViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here the viewController will appear in Portrait (Also all subclass of it)
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Portrait
    }

    override func shouldAutorotate() -> Bool {
        return false
    }

}

class LandscapeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //Here the viewController will appear in Landscape (Also all subclass of it)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Landscape

    }

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

【讨论】:

    猜你喜欢
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多