Last active
June 7, 2022 08:58
-
-
Save dabodamjan/5e1f5b43d6129a86d7774eb0af02e4ff to your computer and use it in GitHub Desktop.
SwiftUI background which covers the full-screen and ignores the safe area
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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