我正在开发一款iPhone的Swift应用程序。在我的应用中有一个模态视图,我只想让它在竖屏模式下显示。
我的问题是,如何通过编程来强制手机不允许旋转?换句话说,我正在寻找一段代码,它将不允许模态视图以横向模式显示(打开竖向旋转锁定)。
这仅适用于一个模态视图,因此我无法关闭整个应用程序的旋转,否则我只会完全禁用旋转。
我在研究中发现了一些代码在这里,但它是Objective-C,如果有帮助的话。谢谢!
我正在开发一款iPhone的Swift应用程序。在我的应用中有一个模态视图,我只想让它在竖屏模式下显示。
我的问题是,如何通过编程来强制手机不允许旋转?换句话说,我正在寻找一段代码,它将不允许模态视图以横向模式显示(打开竖向旋转锁定)。
这仅适用于一个模态视图,因此我无法关闭整个应用程序的旋转,否则我只会完全禁用旋转。
我在研究中发现了一些代码在这里,但它是Objective-C,如果有帮助的话。谢谢!
对于横向左右方向 (更新 Swift 2.0)
并且在信息中有这个
和 UIController
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight]
}
对于PortraitUpsideDown和Portrait使用以下设置:
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false
}
else {
return true
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}
其他解决方案:
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
if visibleViewController is MyViewController {
return true // rotation
} else {
return false // no rotation
}
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
}
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
UIInterfaceOrientation
强制转换为 Int
。它是一个 UInt
值,当前版本的 Swift 现在需要进行强制转换。 - Bill WeinmanUIInterfaceOrientationMask
而不是 UIInterfaceOrientation
。 - redent84shouldAutorotate()
方法中,你应该返回 true
。 - AydinAngoutiSwift 3
如果视图控制器嵌入了UINavigationController或UITabBarController中,则方向旋转会更加复杂,导航栏或选项卡栏控制器会优先并决定自动旋转和支持的方向。
使用以下扩展程序在UINavigationController和UITabBarController上,以便嵌入在这些控制器之一中的视图控制器可以做出决策:
UINavigationController扩展程序
extension UINavigationController {
override open var shouldAutorotate: Bool {
get {
if let visibleVC = visibleViewController {
return visibleVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let visibleVC = visibleViewController {
return visibleVC.preferredInterfaceOrientationForPresentation
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let visibleVC = visibleViewController {
return visibleVC.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
}}
UITabBarController扩展
extension UITabBarController {
override open var shouldAutorotate: Bool {
get {
if let selectedVC = selectedViewController{
return selectedVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let selectedVC = selectedViewController{
return selectedVC.preferredInterfaceOrientationForPresentation
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let selectedVC = selectedViewController{
return selectedVC.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
}}
现在您可以在要锁定的视图控制器中覆盖supportedInterfaceOrientations、shouldAutoRotate和preferredInterfaceOrientationForPresentation,否则您可以在其他视图控制器中省略这些覆盖项,以继承应用程序plist中指定的默认方向行为。
锁定特定方向
class YourViewController: UIViewController {
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
return .portrait
}
}}
禁用旋转
class YourViewController: UIViewController {
open override var shouldAutorotate: Bool {
get {
return false
}
}}
更改首选界面方向以进行演示
class YourViewController: UIViewController {
open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
return .portrait
}
}}
由于您的视图控制器可能属于导航控制器,以上代码可能无法正常工作。如果是,则即使它具有不同的方向规则,它也必须遵守导航控制器的规则。更好的方法是让视图控制器自行决定,而导航控制器将使用顶部视图控制器的决策。
我们可以通过此UINavigationController通用扩展来支持锁定当前方向并自动旋转以在特定方向上锁定:
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
}
现在在您的视图控制器中,我们可以
class ViewController: UIViewController {
// MARK: Autoroate configuration
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return true
}
else {
return false
}
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue)
}
}
希望能对你有所帮助。 谢谢。
这将禁用视图的自动旋转:
override func shouldAutorotate() -> Bool {
return false;
}
更新
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false;
}
else {
return true;
}
}
如果应用程序处于横向模式,并且您显示必须以纵向模式显示的视图,则这将允许应用程序更改其方向为纵向(当设备旋转到该方向时)。如果有人需要答案,我想我刚得到它。尝试这个:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait.toRaw().hashValue | UIInterfaceOrientationMask.PortraitUpsideDown.toRaw().hashValue
}
希望能有所帮助!
编辑:
要强制旋转,请使用以下代码:
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
它适用于iOS 7和8!
Go to your pList and add or remove the following as per your requirement:
"Supported Interface Orientations" - Array
"Portrait (bottom home button)" - String
"Portrait (top home button)" - String
"Supported Interface Orientations (iPad)" - Array
"Portrait (bottom home button)" - String
"Portrait (top home button)" - String
"Landscape (left home button)" - String
"Landscape (right home button)" - String
// UIappViewController.swift
import UIKit
class UIappViewController: UIViewController {
super.viewDidLoad()
}
//Making methods to lock Device orientation.
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// LoginViewController.swift
import UIKit
import Foundation
class LoginViewController: UIappViewController{
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
针对Swift 3和iOS 10
override open var shouldAutorotate: Bool {
return false
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
然而,在当前iOS 9版本中,shouldAutorotate
设置存在一个bug,无法正常工作。
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) | Int(UIInterfaceOrientationMask.LandscapeRight.rawValue)
}
我只有通过修改plist文件和代码的结合才能使其正常工作。