Ignoring Safe Area in SwiftUI

Posted in SwiftUI

March 24th, 2021

⏱ Reading Time: 2 mins

When making iOS applications, it’s often necessary to ignore the safe area of the device and extend content in it. That mostly because we need to expand a background color, an image, or any other SwiftUI view in order to end up with a unified background that won’t be looking broken by the safe area. As you will see in this quick post, it’s really easy to achieve that.

Let’s start with a really simple example; the following color that stops in the safe area bounds:

Color view with default safe area

See that the top and bottom edges of the safe area remain unaffected. To change that, we can use the ignoresSafeArea view modifier:

Color ignoring all safe area edges

The above modifier returns a new view that ignores all safe area edges and expands to all directions. And it’s all we need to do the trick! However, what if we wanted to ignore a specific edge or edges, and not all of them?

Thankfully, ignoresSafeArea has an alternative initializer; it allows to specify the edges we want to ignore:

Color ignoring bottom safe area edge only

By setting the .bottom value as the second argument in the above modifier, only the bottom edge is ignored. The original behavior is maintained for the rest.

If more than one edges should be ignored, then the second argument must be an array. For instance, the following ignores bottom and trailing edges:

Note that ignoresSafeArea view modifier is available on iOS 14 and above. That means that if supporting iOS 13 is a requirement this modifier just can’t be used.

However, there is another modifier to apply instead, called edgesIgnoringSafeArea. It expects one argument only; the edge or edges to ignore:

The above has the same results to simply calling the ignoresSafeArea modifier without arguments. It ignores all edges. But similarly as shown before, specific edges can be provided here too:

Important Note: Even though at the time or writing edgesIgnoringSafeArea is the way to ignore safe area edges when supporting iOS 13, it seems that this modifier will become deprecated with the coming of iOS 14.5 according to Apple docs. So be cautious with it.

So, to summarize, ignoring safe areas in SwiftUI is just a matter of calling a simple view modifier. Apply it whenever necessary, and expand the safe area of your views in order build an interface the way you like it.

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.