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 3, 2024 How to open and close windows programmatically in SwiftUI?

To open or close a window programmatically from outside that window using environment variables, you need to leverage the new openWindow (macOS 13+) and dismissWindow (macOS 14+) environment variables. This environment variables allow you to programmatically open and close a window by its identifier.

question swiftui April 2, 2024 How to declare an array of Views in SwiftUI?

In SwiftUI, you can declare an array of View using the standard Swift array syntax. Here’s how you can do it:

question swiftui swift August 31, 2024 @Published in SwiftUI

In SwiftUI, the @Published property wrapper is used in combination with the ObservableObject protocol to automatically announce changes to properties of a class. This allows SwiftUI views that depend on these properties to update automatically when the data changes.