Lea*_*ode 3 picker ios swiftui
我正在尝试实现 UIKit 国家/地区选择器的 SwiftUI 版本。
用户输入一个表单,其中有一个 Picker 对象,该对象应创建包含所有国家/地区以及国旗图像的选取器。
请参阅下面的屏幕截图:
第一个屏幕是您第一次尝试添加评论时的表单
用户单击“原产地国家”选择器对象,它会导航到“国家/地区”视图。** 这就是问题所在,图像无法渲染!?**
用户选择一个国家/地区,然后关闭选取器视图以返回到表单视图,并显示选择,效果完美,显示所选国家/地区的图像和名称。
有谁能弄清楚是否有解决方案,或者这是 iOS 13 的错误!?
代码如下:
Form {
TextField("Name", text: $name)
TextField("Description", text: $desc)
Picker(selection: $countryOrigin, label: Text("Country of Origin")) {
Section(header: SearchBar(text: $fetcher.searchQuery)) {
List(fetcher.country) { country in
HStack() {
Image(uiImage: UIImage(contentsOfFile: Bundle.main.path(forResource: "CountryPicker", ofType: "bundle")! + "/Images/\(country.id).png")!)
.clipShape(Circle())
Text(country.name)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
只需使用选择器
import SwiftUI
struct ContentView: View {
@State var sel = -1
let trashes = ["trash", "trash.fill", "trash.slash", "trash.slash.fill"]
var body: some View {
NavigationView {
Form {
Picker(selection: $sel, label: Text("Trash type")) {
ForEach(0 ..< trashes.count) { (i) in
HStack {
Image(systemName: self.trashes[i])
Text(self.trashes[i])
}.tag(i)
}
}
}.navigationBarTitle("Select trash")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
为了简单起见,我使用了 UPDATE 而不是 SearchBar
import SwiftUI
struct ContentView: View {
@State var sel = -1
let trashes = ["trash", "trash.fill", "trash.slash", "trash.slash.fill"]
var body: some View {
NavigationView {
Form {
Picker(selection: $sel, label: Text("Trash type")) {
Section(header: Text("Header")) {
ForEach(0 ..< trashes.count) { (i) in
HStack {
Image(systemName: self.trashes[i])
Text(self.trashes[i])
}.tag(i)
}
}
}
}.navigationBarTitle("Select trash")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7885 次 |
最近记录: |