我点击选择器,它将打开屏幕上的元素列表,我们可以添加搜索栏吗?
我实现了国家选择器,在国家列表中,我显示国家名称和国家代码,以便在列表屏幕上添加搜索栏,轻松找到国家。
struct ContentView: View {
//All Country get from the plist with country Code and Coutnry Name.
let countyList = Country().getAllCountyInfo()
// Selected Country
@State private var selectedCountry = 0
var body: some View {
NavigationView {
Form {
Picker(selection: $selectedCountry, label: Text("Country")) {
ForEach(countyList) { country in
HStack{
Text(country.countryName ?? "")
Spacer()
Text(country.countryCode ?? "")
}
}
}
}
.navigationBarTitle("Select country picker")
}
}
}
通过运行上面的代码,它将打开一个国家列表,如上面的屏幕。
在上面的屏幕上(国家列表)。
如何添加搜索栏以过滤国家/地区数据?
2条答案
按热度按时间jm81lzqq1#
我在我的博客文章中解决了这个问题:https://roddy.io/2020/09/07/add-search-bar-to-swiftui-picker/
创建
UIViewRepresentable
:然后很容易将其嵌入到
Picker
中:jljoyd4f2#
您可以添加一个
TextField
,它将作为选择器主体中的搜索栏:搜索条形码:
带搜索栏的选取器: