使用Swift在一个ViewController中强制使用横向模式

saz*_*azz 60 landscape ios swift

我试图在我的应用程序中强制只有一个视图在横向模式,我打电话

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

override func supportedInterfaceOrientations() -> Int {
    print("supportedInterfaceOrientations")
    return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.LandscapeLeft
}
Run Code Online (Sandbox Code Playgroud)

视图以纵向模式启动,并在更改设备方向时保持旋转.
永远不会调用shouldAutorotate.
任何帮助,将不胜感激.

saz*_*azz 69

它可能对其他人有用,我找到了一种强制视图以横向模式启动的方法:

把它放在viewDidLoad()中:

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

和,

override var shouldAutorotate: Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

  • 我刚尝试使用它,它没有用; 虽然改变了shouldAutorotate()来返回false工作.需要明确的是:无论如何,我希望它留在肖像中.(当然也改变了UIInterfaceOrientation.LandscapeLeft到UIInterfaceOrientation.Portrait) (4认同)
  • 这是因为您的视图以纵向模式加载并且您希望它保持这样,我的视图以纵向模式加载并且我希望它以横向模式显示。如果我将 shouldAutorotate 设置为 NO,它将不会在横向中显示 (2认同)
  • 这会导致Apple拒绝您的应用吗?orientation是一个只读属性,你正在操纵该值.我认为苹果会对此不以为然. (2认同)

Man*_*ios 47

斯威夫特4

override func viewDidLoad() {
    super.viewDidLoad()
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeLeft
}

override var shouldAutorotate: Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

//如果您的视图嵌入在导航控制器中,则仅上述操作无效.你必须级联//所以在类定义后添加以下扩展名

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)

斯威夫特3

override func viewDidLoad() {
    super.viewDidLoad()
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.landscapeLeft
}

private func shouldAutorotate() -> Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

  • 这个作品很迷人,谢谢! (2认同)
  • 此代码在iOS 12上不起作用。应用程序崩溃,并显示以下错误:“支持的方向与应用程序没有共同的方向,[PlayerViewController shouldAutorotate]返回YES” (2认同)

Zee*_*sha 23

Swift 4,在iOS 11中测试过

您可以在projectTarget中指定方向- >常规 - > DeploymentInfo(设备方向) - >纵向(Landscapeleft和Landscaperight是可选的)

AppDelegate中

    var myOrientation: UIInterfaceOrientationMask = .portrait
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return myOrientation
    }
Run Code Online (Sandbox Code Playgroud)

LandScpaeViewController

override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.myOrientation = .landscape
}
Run Code Online (Sandbox Code Playgroud)

OnDismissButtonTap

let appDelegate = UIApplication.shared.delegate as! AppDelegate
 appDelegate.myOrientation = .portrait
Run Code Online (Sandbox Code Playgroud)

而已.:)

  • 仅当设备旋转一次时,才会调用supportedInterfaceOrientationsFor。 (2认同)

小智 18

使用Swift 2.2

尝试:

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

其次是:

UIViewController.attemptRotationToDeviceOrientation()
Run Code Online (Sandbox Code Playgroud)

来自Apple的UIViewController类参考:

某些视图控制器可能希望使用特定于应用程序的条件来确定支持哪些接口方向.如果您的视图控制器执行此操作,当这些条件发生更改时,您的应用程序应调用此类方法.系统立即尝试旋转到新方向.

然后,正如其他人建议的那样,根据需要覆盖以下方法:

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

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

我有一个签名视图的类似问题,这解决了我.


Ela*_*van 15

在 AppDelegate 中添加:

//Orientation Variables
var myOrientation: UIInterfaceOrientationMask = .portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return myOrientation  
}
Run Code Online (Sandbox Code Playgroud)

在想要改变方向的 viewController 中添加这个:

override func viewDidLoad() {
        super.viewDidLoad()
        self.rotateToLandsScapeDevice()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.rotateToPotraitScapeDevice()
    }

    func rotateToLandsScapeDevice(){
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.myOrientation = .landscapeLeft
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true)
    }

    func rotateToPotraitScapeDevice(){
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.myOrientation = .portrait
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true)
    }
Run Code Online (Sandbox Code Playgroud)

  • 有人遇到此解决方案的错误吗?如果我旋转到横向,然后使用应用程序任务管理器在设备上选择不同的应用程序,然后导航回我的应用程序,然后单击“返回”。方向保持横向。 (2认同)

Mic*_*bob 10

对我来说,最好的结果来自Zeesha的答案和sazz的答案.

将以下行添加到AppDelegate.swift:

var orientationLock = UIInterfaceOrientationMask.portrait
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return myOrientation
}  
Run Code Online (Sandbox Code Playgroud)

将以下行添加到视图控制器类:

let appDel = UIApplication.shared.delegate as! AppDelegate
Run Code Online (Sandbox Code Playgroud)

将以下行添加到视图控制器中viewDidLoad():

appDel.myOrientation = .landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)

(可选)将此行添加到您的解散功能:

appDel.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)

这些代码行的作用是将默认方向设置为纵向,在视图控制器加载时将其旋转,然后在视图控制器关闭后最终将其重置为纵向.

  • 这也对我有用,因为在我的项目->部署信息中,我只是选择了肖像模式。谢谢 (2认同)

Yar*_*ych 7

覆盖(在 ViewController 中):

override public var shouldAutorotate: Bool {
    return false
} 

override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscapeRight
}

override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .landscapeRight
}
Run Code Online (Sandbox Code Playgroud)

ios 13 的提示。从 ios 13 开始,VC 具有不同的modalPresentationStyleas.automatic和设备呈现模式视图,而不是 Full-Screen VC。要解决这个问题,必须设置modalPresentationStyle.fullScreen. 例子:

let viewController = YourViewController()
viewController.modalPresentationStyle = .fullScreen
Run Code Online (Sandbox Code Playgroud)

  • viewController.modalPresentationStyle = .fullScreen 帮助了我。谢谢! (2认同)

Rob*_*man 6

我需要强制一个控制器进入纵向.添加这个对我有用.

iOS 11的swift 4

override var   supportedInterfaceOrientations : UIInterfaceOrientationMask{

    return  .portrait

}
Run Code Online (Sandbox Code Playgroud)