Screen orientation - SwiftUI

In SwiftUI, make sure all UIInterfaceOrientationMask values are used for the UISupportedInterfaceOrientations key inside the Info.plist file.

To listen to orientation changes, subscribe to orientationDidChangeNotification in the onReceive method. Check the device orientation using UIDevice.current.orientation.

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
@State var currentOrientation = UIDevice.current.orientation

private let orientationChangedNotification = NotificationCenter.default
    .publisher(for: UIDevice.orientationDidChangeNotification)
    .makeConnectable()
    .autoconnect()

var body: some View {
    OrientationAdaptiveView()
        .onReceive(orientationChangedNotification) { _ in
            // Reach to device orientation change
            self.currentOrientation = UIDevice.current.orientation
        }
}