How can I disable landscape orientation?

后端 未结 9 2082
忘掉有多难
忘掉有多难 2020-12-02 22:53

I am making an iPhone app and I need it to be in portrait mode, so if the user moves the device sideways, it does not automatically rotate. How can I do this?

相关标签:
9条回答
  • 2020-12-02 23:32

    Most simple solution separate for iPhone and iPad (Universal) - its remove unnecessary orientation in the info.plist file or Project -> Info -> Custom iOS Target Properties.

    Just add or remove orientation item from list:

    • Supported interface orientation for iPhone
    • Supported interface orientations (iPad) for iPad

    0 讨论(0)
  • 2020-12-02 23:36

    Xcode 4 and below

    For those who missed it: you can use the project settings screen to fix orientations throughout the app (no need to override methods in every controller):

    enter image description here

    It's as simple as toggling the supported interface orientations. You can find by clicking on your Project in the left panel > the app target > Summary tab.

    0 讨论(0)
  • 2020-12-02 23:36

    If you want the application to be running only in Portrait mode,

    Below your viewDidLoad method copy the below line of code

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
       get {
          return .portrait
       }
    }
    

    This will lock the landscape mode for your application. Similarly by replacing .portrait to .landscape will run your application in landscape mode.

    If not specified your application will run on both modes.

    Alternatively, you can lock the orientation from Xcode Builder as shown below.

    0 讨论(0)
提交回复
热议问题