Skip to content
DeveloperMemos

Detecting Device Orientation with SwiftUI

SwiftUI, Device Orientation, iOS Development1 min read

As mobile devices continue to evolve, creating responsive and adaptive user interfaces becomes increasingly important. Whether you want to adjust the layout of your app's interface or trigger specific actions based on device orientation, SwiftUI offers straightforward solutions for detecting and responding to these changes.

Understanding Device Orientation

Before delving into the implementation aspect, it's crucial to understand device orientation. In the context of iOS devices, orientation refers to whether the device is in a portrait or landscape mode. This information is valuable, as it allows us to tailor the user experience accordingly.

Detecting Device Orientation

With SwiftUI, detecting the device orientation involves leveraging environment values, specifically the horizontalSizeClass and verticalSizeClass provided by the @Environment property wrapper. These size classes correspond to the width and height of the available screen space, which dynamically change when the device rotates.

Let's consider an example where we want to alter the layout of a view based on the device's orientation. We can achieve this by utilizing the @Environment property wrapper along with a GeometryReader to access the size of the available space. Here's a simplified demonstration:

1struct ContentView: View {
2 @Environment(\.horizontalSizeClass) var horizontalSizeClass
3 @Environment(\.verticalSizeClass) var verticalSizeClass
4
5 var body: some View {
6 GeometryReader { geometry in
7 if horizontalSizeClass == .compact && verticalSizeClass == .regular {
8 // Adjust layout for portrait orientation
9 Text("Portrait Mode")
10 .frame(width: geometry.size.width, height: geometry.size.height)
11 } else {
12 // Adjust layout for landscape orientation
13 Text("Landscape Mode")
14 .frame(width: geometry.size.width, height: geometry.size.height)
15 }
16 }
17 }
18}

In this example, we observe how the use of @Environment enables us to detect and respond to changes in device orientation effectively.

Reacting to Orientation Changes

Upon detecting device orientation, you may want your app to dynamically update its appearance or behavior. To achieve this, SwiftUI provides the onReceive modifier, which allows you to subscribe to notifications related to orientation changes.

Here's a brief illustration showing how we can use onReceive to react to orientation changes:

1struct OrientationSensitiveView: View {
2 @State private var currentOrientation = UIDevice.current.orientation
3
4 var body: some View {
5 Text("Current Orientation: \(currentOrientation.isLandscape ? "Landscape" : "Portrait")")
6 .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
7 self.currentOrientation = UIDevice.current.orientation
8 }
9 }
10}

In this case, the onReceive modifier listens for the UIDevice.orientationDidChangeNotification and updates the view based on the current orientation.