【问题标题】:How to show last index as default in Hstack SwiftUI如何在 Hstack SwiftUI 中将最后一个索引显示为默认值
【发布时间】:2021-07-07 08:10:41
【问题描述】:

我写了下面的代码,并在scrollview上将数据显示到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)

【问题讨论】:

    标签: swift swiftui calendar scrollview hstack


    【解决方案1】:

    使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多