如何在 iOS 中以编程方式锁定屏幕方向?


你可能会遇到需要将 UI 显示在特定方向(可能是横屏或竖屏)的情况下。

我们将了解如何使用 iOS 中的 Swift 以编程方式锁定方向。

打开 Xcode → 新项目 → ViewController.swift 编写以下代码。

// Set the shouldAutorotate to False
override open var shouldAutorotate: Bool {
   return false
}

// Specify the orientation.
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
   return .portrait
}

更新于: 2019 年 7 月 30 日

2K+ 浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告