如何在Swift中以编程方式更改设备方向? [英] How to change Device Orientation programmatically in Swift?

查看:83
本文介绍了如何在Swift中以编程方式更改设备方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我想更改设备方向.

In my app I want to change the device orientation.

我已经尝试了另一个主题相同的问题:

I`ve tried it like in the other question with the same topic:

let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

但是,这似乎只有在已经允许了方向的情况下才起作用,以便用户可以通过旋转设备来更改它,并且还可以通过动画来更改方向.但是我希望我的应用在VC1中只有一种方向,而在VC2中只有一种方向.用户不应影响方向,也不应有动画.这可能吗?

But this seems only to work if the orientation is already allowed so that the user can change it with rotating the device, and it also change the orientation with an animation. But I want my app to only have one orientation in VC1 and to have only one orientation in VC2. The user should not have influence on the Orientation, and there should no animation. Is this possible?

对不起,我的英语不好....

Sorry for my bad english....

推荐答案

对于每个VC,请声明此变量w/所需的方向.这是用于肖像的.

For each VC declare this variable w/ desired orientation. This is for portrait.

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }   

然后在外观上强制执行所需的方向.

Then on appearance enforce the desired orientation.

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    UIView.setAnimationsEnabled(false)
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
    UIView.setAnimationsEnabled(true)
  }

这篇关于如何在Swift中以编程方式更改设备方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆