Reputation: 868
I'm pretty new with SwiftUI and I'm encountering a really unnerving issue.
I've created some custom views that are being shown with the right height inside the preview. When I try to add them inside a VStack, they behave instead like I'd set .frame(minHeight: 0, maxHeight: .infinity)
. Adding a Spacer()
after the custom view, inside the VStack
, do not solve the issue.
I don't know the size of those views in advance so I can't set a fixed frame size, is there anything I can do to prevent this behaviour? I even thought about calculate their content size and apply a fixed size accordingly, but it sounds like a developer nightmare :|
Thanks for reading, and for the help!
EDIT: The most simple code that raises the issue:
VStack {
ChipGroup(chips: [
Chip(titleKey: "Chip"),
Chip(titleKey: "Chip"),
Chip(titleKey: "Chip"),
Chip(titleKey: "Chip"),
Chip(titleKey: "Chip"),
Chip(titleKey: "Chip"),
Chip(titleKey: "Chip")
])
Spacer()
}
Chip & ChipGroup are based on the following tutorial: https://prafullkumar77.medium.com/swiftui-building-chips-with-autolayout-container-dbca53bbb848
Upvotes: 0
Views: 679
Reputation: 3053
GeometryReader is the root view of your ChipGroup. GeometryReader always takes up the most space possible, expanding as much as possible.
If you don't want your view to take up all the space, you need to get rid of the GeometryReader - unless you want to set a fixed height, which you specifically mentioned you'd like to avoid. That's just how GeometryReader works.
Upvotes: 2