Skip to content
DeveloperMemos

How to Get the Safe Area Height in SwiftUI

SwiftUI, Safe Area, Layout1 min read

If you've been working with SwiftUI for some time, you might have encountered the need to position elements within the safe area of the screen. This becomes particularly important when you want to ensure that your content is not obstructed by system-provided elements such as the notch, home indicator, or any other platform-specific adornments. In this article, we'll explore how to obtain the safe area height in SwiftUI, ensuring your app's content is properly positioned within the safe area of the screen.

Understanding the Safe Area

Before delving into obtaining the safe area height, it's crucial to understand what the safe area is and its significance. The safe area represents the portion of the screen that is free from obstructions, ensuring that the content you place within it remains visible and accessible, regardless of the device's form factor.

In SwiftUI, handling the safe area is essential for creating adaptive and visually appealing user interfaces across a wide range of Apple devices. It ensures that your app's content is comfortably displayed without being obscured by system elements.

Obtaining Safe Area Height in SwiftUI

To obtain the safe area height in SwiftUI, we can leverage the GeometryReader view and the GeometryProxy object. The GeometryReader allows us to read the size and position of a view, while the GeometryProxy provides access to the reader's geometry within the closure.

Here’s an example of how you can obtain the safe area height using SwiftUI:

1import SwiftUI
2
3struct ContentView: View {
4 var body: some View {
5 GeometryReader { geometry in
6 Color.clear
7 .onAppear {
8 let safeAreaHeight = geometry.safeAreaInsets.top + geometry.safeAreaInsets.bottom
9 print("Safe area height: \(safeAreaHeight)")
10 }
11 }
12 }
13}

Adapting Layout Using Safe Area Height

Once you have obtained the safe area height, you can utilize it to adapt the layout of your SwiftUI views accordingly. For instance, you might want to adjust the positioning of certain elements to ensure they are within the safe area bounds, providing a consistent experience across different devices.

Let's consider a scenario where you want to create a full-screen view with a background color that extends into the safe area:

1import SwiftUI
2
3struct FullScreenView: View {
4 var body: some View {
5 GeometryReader { geometry in
6 Color.blue
7 .edgesIgnoringSafeArea(.all)
8 .overlay(Text("This is a full-screen view"))
9 }
10 }
11}

By leveraging the safe area height, you can make informed decisions about the placement and sizing of your views, catering to the specifics of each device's display.

In Closing

Understanding how to retrieve the safe area height in SwiftUI is fundamental for building responsive and adaptive user interfaces. By incorporating the safe area considerations into your layout, you can ensure that your app's content is intelligently positioned within the safe area, offering a consistent and polished experience across various Apple devices.