Why does my SwiftUI page title change when a picker option is selected?

不想你离开。 提交于 2020-07-22 06:25:29

问题


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


回答1:


Answering my own question, this problem is solved by wrapping the Form in a Section and defining the navigationBarTitle on it.

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

I got the idea from this answer, although I've no idea why the title needs to be defined twice.



来源:https://stackoverflow.com/questions/62868699/why-does-my-swiftui-page-title-change-when-a-picker-option-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!