繁体   English   中英

如果应用程序设备的方向是纵向的,那么如何仅在iOS中为一个视图构建者设置强制横向,Swift 2.3

[英]if app device orientation is portrait how to set force landscape only for one view constroller in ios, swift 2.3

对于我的项目,设备方向为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
    }

那么会导致崩溃。 对我没有任何作用。 任何想法如何做到这一点。

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

在此处输入图片说明

我怎样才能做到这一点。 希望您对此有所帮助。

进行所需操作的最佳方法是在应用程序设置中允许所有方向

然后将所有的viewControllers 设置为“ should autorotate”变量为true

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

然后根据您的ViewContoller设置supportedInterfaceOrientations

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

}

要么

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

例如,这里我有两个viewcontrollers,当调用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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM