Start a Project

Lock Screen Orientation in Swift

There are a lot of apps nowadays which we want to use in only landscape or portrait mode according to our preference. Today we are going to implement lock screen orientation in swift.

We can set orientation according to our requirement i.e. portrait or landscape. Let’s check how can we implement lock screen orientation in swift.

Step 1: Create a new iOS project & select device orientation as per the requirement in Runner.

Step 2: Add supported Orientation method in App Delegate.

Step 3: Create 2 buttons for selecting the landscape & portrait orientation.

Step 4: Create action outlets for these buttons in view controller.

Step 5: Create a global helper class to adjust the lock & rotate it to the desired orientation.

Step 6: Use lockOrientation method on the button action in the view controller to lock orientation & rotate the screen.

OUTPUT:

https://mobikul.com/wp-content/uploads/2023/03/Screen-Recording-2023-03-31-at-12.27.27-PM.mov

CONCLUSION:

In this article, I have explained how to lock screen orientation, which you can modify and experiment with according to your own.

Thanks for reading this article ❤

If I got something wrong 🙈, let me know in the comments. I would love to improve.

For more interesting blogs check out here – https://mobikul.com/blog/

Reference link: https://www.polpiella.dev/changing-orientation-for-a-single-screen-in-swiftui

Exit mobile version