简体   繁体   中英

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.

从选取器查看

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM