Created
October 1, 2022 10:20
-
-
Save csknns/7e69619a8b94e770b97a68024feff405 to your computer and use it in GitHub Desktop.
allow declaring SwiftUI view modifiers at the begining of the view instance
This file contains 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
// Example of a convient method to allow declaring | |
// the view modifiers at the begining of the view instance | |
// instead at the end. | |
extension VStack { | |
static func with<T: View>(modifiers: (Self) -> T, alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: () -> Content) -> some View { | |
modifiers(VStack(alignment: alignment, | |
spacing: spacing, | |
content: content)) | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
VStack.with(modifiers: { | |
$0.padding() | |
}) { | |
Text("Hello, world!") | |
.padding() | |
Text("Hello, world!") | |
.padding() | |
Text("Hello, world!") | |
.padding() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment