For MacOS I use the following code many times to create forms. I really like to create a modifier or extension to make it more simplified.
HStack{
VStack (alignment: .leading, spacing: 5) {
Text("Account Number").font(.headline)
TextField("", text: $selectedBankAccount.accountNumber, prompt: Text("AccountNumber"))
}
}
.frame(width: 450 )
I would love to see something like:
struct FormStackModifier: ViewModifier {
var width : CGFloat
func body(content: Content) -> some View {
HStack {
VStack(alignment: .leading, spacing: 5) {
content
}
}
.frame(width: width)
}
}
// and use it like
ModifiedContent(
content: VStack { Text("Account Name").font(.headline)
TextField("", text: $selectedBankAccount.accountName, prompt: Text("AccountName"))},
modifier: FormStackModifier(width: 450)
)
But I need stil the extra VStack in content.
Is there a to solve this and a way to make this neat?
Groupis a view that does nothing but groups views together, allowing you to use multiple views as a single view. UnlikeVStack, it doesn't enforce a particular layout on the views or do anything like that.You can replace the
VStackwithGroup.Using
ModifiedContentlike this is rather inconvenient. I recommend adding an extension onViewthat applies theFormStackModifier.Alternatively, instead of a
ViewModifier, you can make your ownViewcalledFormStack: