Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

ios - SwiftUI Picker Focus on selected item in view

I'm trying to have the selected item in the Picker display at the top of the view. I have added. standard picker:

Section(){
    Picker(selection: $selectedYear, label: Text("")) {
        ForEach(2000...2050, id: .self) {
            Text(String($0))
        }
    }
}

2020 is currently selected, however when the view appears it displays from 2000 at the top, 2020 outside of the screen with a check next to it.

View from Picker

question from:https://stackoverflow.com/questions/65546164/swiftui-picker-focus-on-selected-item-in-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Pickers in SwiftUI need a tag to work.

Section(){
    Picker(selection: $selectedYear, label: Text("")) {
        ForEach(2000...2050, id: .self) {
            Text(String($0)).tag($0) // <= add this to work
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...