简体   繁体   English

SwiftUI 选择器 聚焦于视图中的选定项目

[英]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.当前选择了 2020,但是当视图出现时,它会在顶部显示 2000,在屏幕外部显示 2020,旁边有一个勾号。

从选取器查看

Pickers in SwiftUI need a tag to work. SwiftUI 中的拾取器需要一个标签才能工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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