Search code examples
iosswiftswiftuiswiftui-form

SwiftUI First object in a Group overlaps


I decided to give SwiftUI a go but I seem to be having a bit of a blonde moment here. All I did was create a Form to which I added few Text objects. Due to the limit of 10 per parent view, I added the objects to Groups. When adding new Groups inside the same Form, I noticed that the first object of the Group gets added behind all the objects in the Group above as well. Seems like a weird feature, but it's entirely possible that I'm just being thick here.

I can get rid of the overlapping by adding a Spacer, an empty string or changing the Group to a Section for example. But what would be the solution if I want it to be one long list without any separators?

SwiftUI Text in Group in Form SwiftUI Text in Group in Form SwiftUI Text in Group in Form


Solution

  • Group is not a container, imaging it as a transparent thing, which just inserts its content at the top of the available space. So you have two consequent groups, so from layout perspective the top starts for them all at the beginning of Form. That's why group content views overlap, and that's why they are not if you insert some separator between groups - top of each group proposed by Form is different.

    In your use-case I would recommend to use VStack instead of Group.