How to ignore Safe Area edges in SwiftUI and create full-bleed background layouts

In SwiftUI, the Safe Area is the region of the screen that is visible and safe for displaying content. By default, SwiftUI automatically adjusts the layout of your views to take the Safe Area into account, which can be helpful for avoiding layout issues. However, in some cases, you may want your background to extend to the edges of the screen, ignoring the Safe Area.

In this tutorial, you'll learn how to create full-bleed background layouts in SwiftUI by ignoring the Safe Area edges. We'll explore different ways to implement this, such as by using a ZStack or by adjusting the view's frame. You'll also learn how to handle safe area insets when necessary, such as for displaying content that should be inset from the edges of the screen. With this knowledge, you'll be able to create stunning and immersive layouts in your SwiftUI apps.

SwiftUI automatically makes your content fit within the bounds of the safe area. For background elements and some layout cases, you can ignore those edges by using ignoreSafeArea.

What is the Safe Area?  

To understand how Safe Area works in iOS, I suggest reading about Adaptivity and Layout in the Apple Human Interface Guidelines. It is the content space that does not overlap with the status bar, notch and home indicator spaces.

Ignore Safe Area  

  • For a custom background, it is typical to apply ignoreSafeArea. 
  • By default, it's going to apply to all sides: top, bottom, leading, trailing, all at once.

Color.blue.ignoresSafeArea()

..

Safe Area Edges  

If you want to ignore only a single edge, you can customize the edges value to 

.top

.bottom

.leading or .trailing.

Color.blue.ignoresSafeArea(.all, edges: .top)

..

Avoid Applying to Content  

  • Safe Area helps you tremendously to deal with UI issues that come with notch and home indicator spaces
  • It is recommended to avoid applying ignoreSafeArea to the content. 
  • Instead, you should separate your elements that ignore the safe area, such as a background element.

Comments