Swift and SwiftUI tutorials for Swift Developers

What is .ignoresSafeArea() in SwiftUI

iOS has a concept known as safe areas to help with the layout of views. Safe areas help ensure that views are placed within the visible portion of the interface. For example, safe areas prevent views from obscuring the status bar. If your UI includes a navigation bar, the safe area will automatically adjust to prevent you from placing views that would otherwise obscure the navigation bar.

To position content that extends outside of safe areas, you can use the ignoresSafeArea modifier. For example:

.ignoresSafeArea(.all, edges: .top)

In the following code we can see this modifier in action:

import SwiftUI

struct ContentView: View {
    var body: some View {

		Color.blue
		.ignoresSafeArea()
	}
}

In XCode the result is as follows:

The ignoresSafeArea modifier can accept other values ​​such as .bottom and .leading for the edges parameter. If you want to completely ignore the safe area you can use .ignoresSafeArea() .

Note that the ignoresSafeArea modifier is available since iOS 14. If you are using iOS 13 or earlier you can use the edgesIgnoringSafeArea modifier which only expects one argument, the edge or edges to ignore. For example:

.edgesIgnoringSafeArea(.all)

In short, ignoring safe areas in SwiftUI is just a matter of calling a simple view modifier. Apply it when needed and extend the safe area of ​​your views to create an interface.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

How to hide the Disclosure Indicator in SwiftUI

Next Article

How to build a Custom Back Button in SwiftUI

Related Posts