为什么选择选择器选项后我的 SwiftUI 页面标题会更改? [英] Why does my SwiftUI page title change when a picker option is selected?

查看:28
本文介绍了为什么选择选择器选项后我的 SwiftUI 页面标题会更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

回答我自己的问题,这个问题是通过将 Form 包装在一个 Section 中并定义navigationBarTitle 就可以了.

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.

这篇关于为什么选择选择器选项后我的 SwiftUI 页面标题会更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆