I want to create a ViewModifier, which when applied to a VStack it makes its style look like a List inside a Form, as presented in the image below. I have multiple custom scenarios where Lists don't behave as I need them to, reason why I need this.
To recreate the image I used to following code (I actually used TextFields, but in the example Texts will do)
Form {
Section {
List {
Text("Group name")
Text("Group hashtags")
Text("Group description")
}
}
}
I know how to create modifiers for specific views, but I don't know how to retrieve the list of inner views inside the VStack in order to process them. Once I can retrieve them, I will need to apply a leading padding for the whole content, and place a divider between all views.
Of course I can already do this manually, but a modifier would be better as I can apply all the styling there, maybe also rounding the corners and applying the outer padding.
The final result should look like this:
VStack {
Text("Group name")
Text("Group hashtags")
Text("Group description")
}
.modifier(ListStyleModifier())

VStack. In terms of the idea of adding a style modifier, you could, for example, use the modifier to inject an environment key that would describe the style type, but you'd need custom components inside the stack to respond to them. That solution also doesn't give one access to the index of the item, making it impossible to do things like style the top and bottom items differently. This type of thing seems to be an uphill battle in SwiftUI (right now).