struct SettingsView: View {
let settings: [Setting] = [
Setting(name: "Aperture Increments", options: ["1/3", "1/2", "1"]),
Setting(name: "Shutter Speed Increments", options: ["1/3", "1/2", "1"]),
Setting(name: "ISO Increments", options: ["1/3", "1/2", "1"])
]
var body: some View {
NavigationView {
Form {
ForEach(self.settings, id: \.name) { setting in
SettingDetailView(setting: setting)
}
}
.navigationBarTitle("Settings", displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
SettingsView()
}
}
struct SettingDetailView: View {
let setting: Setting
@State var selection: String = ""
var body: some View {
Picker(selection: $selection, label: Text(setting.name)) {
ForEach(self.setting.options, id: \.self) { option in
Text(option).tag(option)
}
.navigationBarTitle(Text(setting.name), displayMode: .inline)
}
}
}
答案 0 :(得分:1)
回答我自己的问题,可以通过将Form
包装在Section
中并在其上定义navigationBarTitle
来解决。
Form {
Section {
...
}.navigationBarTitle("Settings", displayMode: .inline)
}.navigationBarTitle("Settings")
我从this answer中得到了这个主意,尽管我不知道为什么标题需要定义两次。