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.

Ram Patra Published on April 2, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift September 30, 2023 How to open the Settings view in a SwiftUI app on macOS 14.0 (Sonoma)?

In macOS 14.0 (Sonoma), Apple removed support for NSApp.sendAction to open the Settings view in your SwiftUI app. You now have to use SettingsLink like below:

question swiftui March 29, 2024 How to group different style modifiers and reuse them across multiple SwiftUI views?

In SwiftUI, you can create custom view modifiers to encapsulate common styling configurations and reuse them across different views. Here’s how you can create and reuse a custom view modifier: