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

iosapps/applicationsmobile developmentswift更新于 2026/4/26 19:22:17

您可能会遇到需要将 UI 以特定方向(例如横屏或竖屏)显示的情况。

我们将学习如何使用 Swift 在 iOS 中以编程方式锁定屏幕方向。

打开 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
}



相关文章



关于我们 | 隐私条款 | 学习路线
返回顶部