如何在 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+ 瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告