I can't figure out why my ForEach loop is making my views not scale correctly inside their parent view. If I comment out the ForEach loop and uncomment //.frame(width: geometry.size.width, height: 10)
the RoundedRectangle is scaled correctly in its parent view, but when using the ForEach loop my views flow off the screen. I have double checked that CGFloat((sleepOrAwakeSpan.seconds / DataStore.sleepOrAwakeSpans.map { $0.endTime.timeIntervalSince($0.startTime) }.reduce(0, +)))
total = 100. So I know the geometry is correct, and I know my expected % of the geometry is correct, what could cause the addition of the ForEach to make it flow off screen?
struct AsleepTimeView: View {
@EnvironmentObject var dataStore: DataStore
static let sleepTimeFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
return formatter
}()
var body: some View {
Color.clear.overlay(
GeometryReader { geometry in
VStack(alignment: .center) {
HStack {
ForEach(DataStore.sleepOrAwakeSpans) { sleepOrAwakeSpan in
RoundedRectangle(cornerRadius: 5)
.frame(width: geometry.size.width * CGFloat((sleepOrAwakeSpan.seconds / DataStore.sleepOrAwakeSpans.map { $0.endTime.timeIntervalSince($0.startTime) }.reduce(0, +))), height: 10)
//.frame(width: geometry.size.width, height: 10)
.foregroundColor(sleepOrAwakeSpan.asleep == false ? TrackerConstants.scaleLevel6Color : TrackerConstants.scaleLevel2Color)
//.foregroundColor(.red)
}
}
}
}) // end of overlay
}
}