How to lock screen orientation in swiftui

Asked on 08/04/2024

2 searches

To lock screen orientation in SwiftUI, you can use the supportedInterfaceOrientations modifier. This modifier allows you to specify which orientations your view supports. Here is an example of how you can use it:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .supportedInterfaceOrientations([.portrait])
    }
}

In this example, the view will only support portrait orientation. You can specify multiple orientations by passing an array of UIInterfaceOrientationMask values.

For more detailed information on SwiftUI and its capabilities, you might find the session SwiftUI essentials helpful. This session covers the fundamentals of views and built-in capabilities of SwiftUI.