I have built a Chips Container based on this link. Basically is just a container that orders chips in rows. This is my code:
struct ChipsContent: View {
@ObservedObject var viewModel = ChipsViewModel()
@State private var totalHeight
= CGFloat.zero
var body: some View {
var width = CGFloat.zero
var height = CGFloat.zero
return ScrollView {
GeometryReader { geo in
ZStack(alignment: .topLeading, content: {
ForEach(viewModel.dataObject) { chipsData in
Chips(systemImage: chipsData.systemImage,
titleKey: chipsData.titleKey,
isSelected: chipsData.isSelected)
.padding(.all, 5)
.alignmentGuide(.leading) { dimension in
if (abs(width - dimension.width) > geo.size.width) {
width = 0
height -= dimension.height
}
let result = width
if chipsData.id == viewModel.dataObject.last!.id {
width = 0
} else {
width -= dimension.width
}
return result
}
.alignmentGuide(.top) { dimension in
let result = height
if chipsData.id == viewModel.dataObject.last!.id {
height = 0
}
return result
}
}
}).background(viewHeightReader($totalHeight))
}.padding(.all, 5)
}.frame(height: height)
}
private func viewHeightReader(_ binding: Binding<CGFloat>) -> some View {
return GeometryReader { geometry -> Color in
let rect = geometry.frame(in: .local)
DispatchQueue.main.async {
binding.wrappedValue = rect.size.height
}
return .clear
}
}
}
With this code, the container gets the height according to its content. I want to set a max-height to the Scroll view and if the content is greater than this max height the content should be scrollable. Is it possible to do that?