Disable Auto Rotation in SwiftUI

If you are building your app in SwiftUI using Xcode, you might notice that even if your device’s (maybe an iPhone or iPad) portrait orientation lock is turned on, your app will still rotate with your device rotation.

In this SwiftUI tutorial, I will show you how to prevent or disable the auto-rotation of your App. This is also called orientation disabling.

There’s nothing like adding a line of code to do this if you are using Xcode. You just need to uncheck the unwanted orientation boxes from the Deployment info.

Just follow the steps mentioned in the below image:

disable auto rotation in swiftui

I have written in the screenshot how to turn off orientation in your SwiftUI project.

Steps are:

  • Click on the app name first in your XCode window. (As described in the above image)
  • Then click on your App name from the TARGETS.
  • Check for Deployment Info and uncheck Landscape Left and Landscape Right.

Now try to run your project. It will not auto-rotate your app no matter if your portrait orientation is locked or not.

Note: I have noticed one thing if you check the box of Upside Down, your app will still not rotate upside down. I am not sure why it is happening. For now, I only know that dealing with info.plist and making some changes in it, it should work. Once I learn the proper reason why this does not work, I will be back with another tutorial on that too.

Leave a Reply

Your email address will not be published. Required fields are marked *