0

我正在尝试将 SwiftUI 2 ScrollViewReader 与选择器一起使用,以便点击选择器显示选择器列表以及视图中的当前选择(最好是锚点)。我没有尝试过,包括以下内容,甚至对列表没有任何影响。看起来这应该很简单。

struct ContentView: View {

    @State private var topIntYears: [Int] = createIntYearList()
    @State private var startYear = 1923
    @State private var startDollars: String = "1"

    var body: some View {
        VStack {
            Text("Do Something")
            NavigationView {
                ScrollViewReader { svr in
                    Form {
                        Picker("Base Year", selection: $startYear) {
                            ForEach(topIntYears, id:\.self) {
                                Text(String($0))
                                //this does not work
                                    .simultaneousGesture(
                                        TapGesture()
                                            .onEnded { _ in
                                                print("Picker tapped")
                                                svr.scrollTo(startYear)
                                            }
                                    )
                            }
                        }//pick
                        TextField("Base dollars", text: $startDollars)
                            .keyboardType(.decimalPad)
                            .padding()
                    }//form
                }//svr
            }//nav
        }//v
    }//body
}

func createIntYearList() -> [Int] {
    var years: [Int] = []
    for y in 1898...2021 {
        years.append(y)
    }
    return years
}//int years

Xcode 12.4、iOS 14.4 任何指导将不胜感激。

4

0 回答 0