Skip to content

Instantly share code, notes, and snippets.

@csknns
Created October 1, 2022 10:20
Show Gist options
  • Save csknns/7e69619a8b94e770b97a68024feff405 to your computer and use it in GitHub Desktop.
Save csknns/7e69619a8b94e770b97a68024feff405 to your computer and use it in GitHub Desktop.
allow declaring SwiftUI view modifiers at the begining of the view instance
// 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