当我更改应用程序的语言时,导航标题仅更新一次。当我想再更改一次时,它不会更新。
这个解决方案有效,但它有一个副作用,它会将用户带到应用程序的第一个视图。
这是我的代码:
struct SettingsView: View {
@Environment(LanguageSettings.self) var languageSettings
@AppStorage("isDarkMode") private var isDarkMode = false
@AppStorage("selectedLanguage") private var selectedLanguage: Language = .turkish
var body: some View {
NavigationView {
VStack(content: {
Form {
Section(header: Text("Appearance")) {
Toggle(isOn: $isDarkMode) {
Text("Dark Mode")
}
}
Section(header: Text("Language")) {
Picker("Language", selection: $selectedLanguage) {
ForEach(Array(Language.allCases), id: \ .self) { language in
Text("\(flag(for: language.flagId)) " + language.languageString)
}
}
.onChange(of: selectedLanguage){ languageSettings.language = selectedLanguage }
}
Section(header: Text("Share")) {
Button(action: shareApplication) {
HStack {
Image(systemName: "square.and.arrow.up")
Text("Share Application")
}
}
}
Section(header: Text("Version")) {
Text("1.1.4")
}
}
})
.navigationTitle("Settings")
}
}
}
[[在此处输入图片描述](https://i.sstatic.net/gwjcJ4yI.png)](https://i.sstatic.net/JpulImA2.png)
使用NavigationStack代替NavigationView解决了这个问题。