在 SwiftUI 中更改选择器的导航标题
问题描述:
当用户在 Picker
这是我的选择模型:
enum AppIcon: CaseIterable, Identifiable {
var id: Self {
return self
}
case `default`
case ethereum
case litecoin
var name: String {
switch self {
case .default:
return "Bitcoin"
case .ethereum:
return "Ethereum"
case .litecoin:
return "Litecoin"
}
}
}
这是我的观点
struct ContentView: View {
@State var icon: AppIcon = .default
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $icon, label: Text("Icon")) {
ForEach(AppIcon.allCases) { icon in
Text(icon.name).tag(icon)
}
}
}
}
.navigationBarTitle("Appearance")
}
}
}
我想得到这种行为:
但我试图将 .navigationBarTitle("Title")
放在任何右括号之后,但它不起作用.
but I tried to put .navigationBarTitle("Title")
after any close bracket and it doesn't work.
答
我已尝试解决问题.
struct ContentView: View {
@State var icon: AppIcon = .default
var body: some View {
NavigationView {
Form {
Section {
Picker(selection: $icon, label: Text("Icon")) {
ForEach(AppIcon.allCases) { icon in
Text(icon.name).tag(icon)
}
.navigationBarTitle("Title") // for picker navigation title
}
.navigationBarTitle("Appearance")
}
}
}
}
}