在Swift iOS中设置设备方向 [英] Setting device orientation in Swift iOS

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

问题描述

我正在开发一款适用于iPhone的快速应用程序。在我的应用程序中有一个模态视图,我只想在纵向视图中。

I am working on a swift app for iPhone. There is a modal view in my application that I want only to be in portrait view.

我的问题是,如何以编程方式强制手机不允许旋转?换句话说,我正在寻找不允许以横向模式显示模态视图的代码(打开纵向旋转锁定)。

My question is, how do I programmatically force the phone to not allow rotation? In other words, I am looking for code that will not allow a modal view to be displayed in landscape mode (turning on portrait rotation lock).

这仅适用于1模态视图,所以我无法关闭整个应用程序的旋转,否则我只会完全禁用旋转。

This is just for 1 modal view, so I can't turn off rotation for the entire app, otherwise I would just disable rotation altogether.

我在研究中找到了代码在哪里
但它是在客观C中,如果有帮助的话。谢谢!

I found code in my research here But it is in objective C, in case that helps. Thanks!

推荐答案

您可以将这些方法粘贴到需要为肖像的每个视图的ViewController中:

You can paste these methods in the ViewController of each view that needs to be portrait:

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

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

这篇关于在Swift iOS中设置设备方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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