在Swift iOS中设置设备方向

roc*_*101 63 rotation swift ios8

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

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

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

我在这里的研究中找到了代码 但是在目标C中,如果有帮助的话.谢谢!

Yan*_*eph 69

嗨,适合LandscapeLeft和LandscapeRight (更新Swift 2.0)

在此输入图像描述 你有这个信息

在此输入图像描述

和UIController

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight]
}
Run Code Online (Sandbox Code Playgroud)

对于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]
}
Run Code Online (Sandbox Code Playgroud)

来自法国的消息,圣诞快乐!

编辑:

其他方案:

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())!
    }
}
Run Code Online (Sandbox Code Playgroud)


DrO*_*ild 50

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

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}
Run Code Online (Sandbox Code Playgroud)

  • 如果在调用模态视图时设备处于横向模式,则此代码不起作用.在这些情况下,模态视图以横向模式显示,不能旋转.我该怎么办?但是感谢你的回答,如果我们能够解决这个问题,我会接受它. (13认同)
  • 应该使用`UIInterfaceOrientationMask`而不是`UIInterfaceOrientation` (7认同)
  • 如果您在导航控制器中有VC,如果您只希望将其应用于一个VC而不是全部,那么我认为这不起作用. (4认同)
  • 你应该从`shouldAutorotate()`返回`true`. (3认同)
  • 编辑将`UIInterfaceOrientation`转换为`Int`.这是一个"UInt"值,当前版本的Swift现在需要演员. (2认同)

Dra*_*gos 40

斯威夫特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
    }
 }}
Run Code Online (Sandbox Code Playgroud)

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
    }
}}
Run Code Online (Sandbox Code Playgroud)

现在,您可以覆盖要锁定的视图控制器中的supportedInterfaceOrientations,shouldAutoRotate和preferredInterfaceOrientationForPresentation,否则您可以忽略其他视图控制器中要继承应用程序plist中指定的默认方向行为的覆盖.

锁定特定方向

class YourViewController: UIViewController {
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    get {
        return .portrait
    }
}}
Run Code Online (Sandbox Code Playgroud)

禁用旋转

    class YourViewController: UIViewController {
open override var shouldAutorotate: Bool {
    get {
        return false
    }
}}
Run Code Online (Sandbox Code Playgroud)

更改首选接口方向以进行演示

class YourViewController: UIViewController {
open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
    get {
        return .portrait
    }
}}
Run Code Online (Sandbox Code Playgroud)


Viv*_*har 18

如果您的视图控制器属于导航控制器,则上述代码可能无法正常工作.如果是,则即使它本身具有不同的方向规则,它也必须遵守导航控制器的规则.更好的方法是让视图控制器自行决定,导航控制器将使用最顶层视图控制器的决定.

我们可以支持锁定到当前方向和自动旋转以使用UINavigationController上的这个通用扩展锁定特定方向: - :

extension UINavigationController {
            public override func shouldAutorotate() -> Bool {
                return visibleViewController.shouldAutorotate()
            }

        public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
            return (visibleViewController?.supportedInterfaceOrientations())!
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在在视图控制器中我们可以

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.谢谢


tes*_*sla 11

这将禁用视图的自动旋转:

override func shouldAutorotate() -> Bool {
    return false;
}
Run Code Online (Sandbox Code Playgroud)

更新

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果应用程序处于横向模式并且您显示必须以纵向模式显示的视图,这将允许应用程序将其方向更改为纵向(当然,当设备将旋转到此类方向时).


lob*_*art 11

如果有人想要答案,我想我得到了答案.试试这个:

  • 转到.plist文件并检查所有方向.
  • 在要强制方向的视图控制器中,添加以下代码:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait.toRaw().hashValue | UIInterfaceOrientationMask.PortraitUpsideDown.toRaw().hashValue
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你 !

编辑:

要强制旋转,请使用以下代码:

let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)

它适用于iOS 7和8!


A.G*_*A.G 9

在此输入图像描述

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
Run Code Online (Sandbox Code Playgroud)

注意:此方法允许旋转整个应用程序.

要么

在项目中为UIViewControllers创建一个ParentViewController(继承方法).

//  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.
        }
    }
Run Code Online (Sandbox Code Playgroud)

将每个视图控制器的父控制器关联为UIappViewController.

//  LoginViewController.swift

import UIKit
import Foundation

class LoginViewController: UIappViewController{

    override func viewDidLoad()
    {
        super.viewDidLoad()

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
Run Code Online (Sandbox Code Playgroud)


Cod*_*ide 8

对于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
}
Run Code Online (Sandbox Code Playgroud)

但是,有一个错误,该设置shouldAutorotate目前在iOS 9上不起作用.


小智 6

在info.plist文件中,在"支持的界面方向"中更改所需的方向.

在swift方式支持files-> info.plist->支持界面方向.