3
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)
        }
    }
}

在此处输入图像描述

4

1 回答 1

2

回答我自己的问题,这个问题是通过将 a 包装起来并Form在其上Section定义来解决的navigationBarTitle

Form {
  Section {
    ...
  }.navigationBarTitle("Settings", displayMode: .inline)
}.navigationBarTitle("Settings")

我从这个答案中得到了这个想法,虽然我不知道为什么标题需要定义两次。

于 2020-07-13T04:54:27.680 回答