Reputation: 3588
I am developing a screen using SwiftUI, want to display my view in the top safe area of the screen, is there any method to achieve this in SwiftUI?
Upvotes: 22
Views: 36763
Reputation: 870
Just encountered this question. In SwiftUI 5 the following applies:
struct DrawFullScreen: View {
var body: some View {
Rectangle()
.ignoresSafeArea()
}
}
Try commenting and uncommenting the .ignoresSafeArea() modifier, you will immediately see the view shrink or expand.
Upvotes: 0
Reputation: 222
/**
This extension is needed because of deprecating edgesignoringsafearea for iOS 13.0–15.2
https://developer.apple.com/documentation/swiftui/menu/edgesignoringsafearea(_:)
*/
public extension View {
@ViewBuilder
func expandViewOutOfSafeArea(_ edges: Edge.Set = .all) -> some View {
if #available(iOS 14, *) {
self.ignoresSafeArea(edges: edges)
} else {
self.edgesIgnoringSafeArea(edges) // deprecated for iOS 13.0–15.2, look upper
}
}
}
How to use:
MyView()
.expandViewOutOfSafeArea()
Upvotes: 2
Reputation: 120210
You can use this modifier to pass in the region and the edges you need to ignore
.ignoresSafeArea(.container, edges: .top)
Note: Both parameters are optional
This modifier is very useful when you need to react to the keyboard as the region. The old modifier is not deprecated yet and you can still use it as the primary method:
Use this modifier if need to support iOS 13
.edgesIgnoringSafeArea(.top)
You can pass .all
for both modifiers if you wish to ignore all edges.
Upvotes: 18
Reputation: 1589
By default SwiftUI views will mostly stay inside the safe area. It will go to the bottom of the screen, but it won’t go near any notch at the top of the device. If you want your view to be truly full screen, then you should use the edgesIgnoringSafeArea()
modifier.
struct MyView : View {
var body: some View {
Text("Welcome to Swift UI")
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.edgesIgnoringSafeArea(.top)
}
}
Upvotes: 3
Reputation:
Use this modifier:
.edgesIgnoringSafeArea(.top)
If you want to ignore all the safe area insets you can pass .all
Upvotes: 37