【问题标题】:Change The Orientation Of iOS App Programatically [duplicate]以编程方式更改iOS应用程序的方向[重复]
【发布时间】:2020-12-31 06:40:48
【问题描述】:

当某事发生时,我如何以编程方式更改我的 iOS 应用程序的方向,例如当我按下按钮时,我需要我的设备变为横向,并且在下次单击它时返回纵向。我试过这段代码,但什么也没发生

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

请任何人帮助我!

【问题讨论】:

    标签: ios swift autolayout


    【解决方案1】:

    您可以将以下代码添加到 @IBAction 或您喜欢处理按钮按下的方式中

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

    这里有完整的解决方案https://stackoverflow.com/a/50939184/9065909

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 2017-08-31
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      相关资源
      最近更新 更多