Skip to content

Instantly share code, notes, and snippets.

@dchakarov
Last active January 8, 2021 08:57
Show Gist options
  • Save dchakarov/d841e709ccb0bcc961302b2a248912ec to your computer and use it in GitHub Desktop.
Save dchakarov/d841e709ccb0bcc961302b2a248912ec to your computer and use it in GitHub Desktop.
import SwiftUI
struct BackgroundModifier: ViewModifier {
let colour: Color
func body(content: Content) -> some View {
ZStack {
colour
.edgesIgnoringSafeArea(.all)
content
}
}
}
extension View {
func applyBackground(colour: Color? = nil) -> some View {
modifier(BackgroundModifier(colour: colour ?? Colours.mainBackground))
}
}
@dchakarov
Copy link
Author

dchakarov commented Jan 7, 2021

Usage:

struct ContentView: View {
    var body: some View {
        Text("I love SwiftUI")
            .applyBackground(colour: .orange)
    }
}

@fassko
Copy link

fassko commented Jan 8, 2021

Such a great idea, thanks for showing this! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment