如何在Hstack SwiftUI中将最后一个索引显示为默认值



我在滚动视图上编写以下代码并将数据显示到Hstack中

就我而言,我从一个服务电话中收到了10个项目,并在Hstack中显示了这些项目当用户第一次进入屏幕时,前5天可见,

但我需要先展示最后5次约会。

需要第一次看到最后一个索引(进入屏幕时(。

即需要首先显示当前天数的

HStack(alignment:.center){
GeometryReader{ proxy in
ScrollView(.horizontal) {

LazyHStack {
ForEach(days.indices, id: .self) { i in
CalendarView(
number: days[i].number,
days: days[i].weekday,
color: days[i].isToday ? #colorLiteral(red: 0.9060331583, green: 0.2547450066, blue: 0.3359550834, alpha: 1) : #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1),
textcolor: days[i].isToday ? #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) : #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), proxy: proxy
)
.onTapGesture{
print(days[i])
// this is just for replacing the current selection
for j in days.indices { days[j].isToday = false }
days[i].isToday = true
}
}
}
}
}
}
.padding([.trailing,.leading],3)

使用ScrollViewReader

HStack(alignment:.center){
GeometryReader{ proxy in
ScrollView(.horizontal) {
ScrollViewReader { value in // <== Here
LazyHStack {
ForEach(days.indices, id: .self) { i in
CalendarView(
number: days[i].number,
days: days[i].weekday,
color: days[i].isToday ? #colorLiteral(red: 0.9060331583, green: 0.2547450066, blue: 0.3359550834, alpha: 1) : #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1),
textcolor: days[i].isToday ? #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) : #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), proxy: proxy
)
.id(i) // <== Here
.onTapGesture{
print(days[i])
// this is just for replacing the current selection
for j in days.indices { days[j].isToday = false }
days[i].isToday = true
}
}
}.onAppear(){ // <== Here
value.scrollTo(days.count - 1) // <== Here
}
}
}
}
}
.padding([.trailing,.leading],3)