Loading...

How to force an app or a view to open in landscape only mode in iOS using SwiftUI?

question swiftui iOS
Ram Patra Published on April 2, 2024

In SwiftUI, you can force an app to open in landscape mode by configuring the supported interface orientations in your app’s target settings. Here’s a step-by-step guide to configuring interface orientations in Xcode:

  1. Open your Xcode project.
  2. In the Project navigator, select your app’s target.
  3. Go to the General tab.
  4. Under Deployment Info, you’ll see the Supported Interface Orientations section.
  5. Uncheck the Portrait option and check the Landscape Left and Landscape Right options. This will configure your app to support landscape orientations only.
  6. Save your changes.

xcode-screenshot

By configuring the supported interface orientations in your app’s target settings, you force the app to open in landscape mode regardless of the device’s orientation. However, please note that this approach affects the entire app and cannot be overridden on a per-view basis.

And, if you do not want this behaviour for the entire app and just want this for a specific view then you can achieve this by using the UIViewControllerRepresentable protocol in SwiftUI to wrap a UIKit view controller that supports landscape orientation.

Here’s an example of how you can do it:

  1. Create a SwiftUI view that wraps a UIKit view controller and conforms to UIViewControllerRepresentable:
import SwiftUI

struct LandscapeView: View {
    var body: some View {
        LandscapeViewControllerWrapper()
            .edgesIgnoringSafeArea(.all) // Make sure the view covers the entire screen
    }
}

struct LandscapeView_Previews: PreviewProvider {
    static var previews: some View {
        LandscapeView()
    }
}
  1. Create a UIViewController subclass that supports landscape orientation:
import UIKit

class LandscapeViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Force landscape orientation
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscape
    }
    
    override var shouldAutorotate: Bool {
        return true
    }
}
  1. Create a SwiftUI wrapper for the LandscapeViewController using UIViewControllerRepresentable:
import SwiftUI

struct LandscapeViewControllerWrapper: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> LandscapeViewController {
        return LandscapeViewController()
    }
    
    func updateUIViewController(_ uiViewController: LandscapeViewController, context: Context) {
        // Update the view controller if needed
    }
}

Now, when you navigate to the LandscapeView in your SwiftUI app, it will be displayed in landscape mode while the rest of your app remains unaffected. This approach allows you to control the orientation of individual views within your SwiftUI app.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on April 2, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui macos September 4, 2024 How to execute some code before app termination in a macOS app using SwiftUI?

To run some code before app termination in a macOS app using SwiftUI, the correct approach would involve placing the termination logic within a view, such as the ContentView. Here’s how you can do it:

question swiftui swift October 30, 2023 How to convert Color type to hex and vice-versa while retaining alpha information?

The below should work both on macOS and iOS with one minor change. That is, use UIColor instead of NSColor if you’re planning to use it for iOS.

question swiftui swift September 8, 2024 How to loop through an enum in SwiftUI?

In SwiftUI, looping through an enum is not directly possible without some extra work because enums in Swift don’t inherently support iteration. However, you can achieve this by making the enum CaseIterable, which automatically provides a collection of all cases in the enum.

Like my work?

Please, feel free to reach out. I would be more than happy to chat.