Skip to content

Instantly share code, notes, and snippets.

@dabodamjan
Last active June 7, 2022 08:58
Show Gist options
  • Save dabodamjan/5e1f5b43d6129a86d7774eb0af02e4ff to your computer and use it in GitHub Desktop.
Save dabodamjan/5e1f5b43d6129a86d7774eb0af02e4ff to your computer and use it in GitHub Desktop.
SwiftUI background which covers the full-screen and ignores the safe area
struct FullScreenBackground<BackgroundView: View>: ViewModifier {
@ViewBuilder var backgroundView: () -> BackgroundView
func body(content: Content) -> some View {
ZStack() {
backgroundView().ignoresSafeArea()
content
}
}
}
extension View {
/// Adds a background which ignores safe area. Use it on the first view in the body of the screen.
/// - Parameter backgroundView: A view which should be the background.
/// - Returns: A view with applied background.
func fullScreenBackground<V>(@ViewBuilder backgroundView: @escaping () -> V) -> some View where V: View {
modifier(FullScreenBackground(backgroundView: backgroundView))
}
}
// Usage
SomeView.fullScreenBackground { Color.gray }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment