Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created June 26, 2020 15:52
Show Gist options
  • Save prafullakumar/5102b2f0aed60fbe0a0ff469ebe44a77 to your computer and use it in GitHub Desktop.
Save prafullakumar/5102b2f0aed60fbe0a0ff469ebe44a77 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ChipsContent: View {
@ObservedObject var viewModel = ChipsViewModel()
var body: some View {
var width = CGFloat.zero
var height = CGFloat.zero
return GeometryReader { geo in
ZStack(alignment: .topLeading, content: {
ForEach(viewModel.dataObject) { chipsData in //loop to render all chips
Chips(systemImage: chipsData.systemImage,
titleKey: chipsData.titleKey,
isSelected: chipsData.isSelected)
.padding(.all, 5)
.alignmentGuide(.leading) { dimension in //update leading width for available width
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 //update chips height origin wrt past chip
let result = height
if chipsData.id == viewModel.dataObject.last!.id {
height = 0
}
return result
}
}
})
}.padding(.all, 10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment