【问题标题】:Swift 3 - How to lock orientation for iOS app? [duplicate]Swift 3 - 如何锁定 iOS 应用程序的方向? [复制]
【发布时间】:2016-11-26 06:28:07
【问题描述】:

我正在尝试创建一个只支持纵向的应用程序,我尝试在 Target -> General -> Deployment Info -> Portrait

进行设置

然后,在 appDelegate.swift

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

AT ViewController.swift

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

但它不起作用..在 ViewController.swift 中,它给了我 错误

方法不会覆盖其超类中的任何方法

【问题讨论】:

  • 选择一个目标并在 Deplayment info 下找到 Device Orientation。
  • 您是否取消选中了部署信息中除纵向之外的所有其他方向?
  • 是的,我什至清除了 info.plist 中的风景 --> 支持的界面方向
  • 在 Swift 3 中,这个函数被一个属性所取代。 override var supportedInterfaceOrientations : UIInterfaceOrientationMask { return UIInterfaceOrientationMask.portrait }(注意 var 而不是 func)
  • @JohnFoong,查看我的帖子here 它会帮助你。

标签: ios swift uiimageview


【解决方案1】:

在 Swift 3.1 中

如果你想在整个屏幕上锁定方向。

另见https://freakycoder.com/ios-notes-5-how-to-application-orientation-abba750bed6e

打开 AppDelegate.swift,并添加此代码。

// Lock the orientation to Portrait mode
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue)
}

// Lock the orientation to Landscape(Horizontal) mode
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.landscape.rawValue)
}

【讨论】:

    【解决方案2】:

    这是一个简单的代码,只需将以下代码复制并粘贴到您要为其修复方向的 ViewController 中。

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

    当且仅当所有代码都在 ViewController 中时才有效,无需更改任何内容或将任何内容放入 AppDelegate。干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多