[英]Default text for back button in NavigationView in SwiftUI
I use a NavigationLink
to navigate from "View1" to "View2", on the second view, the back button gets the title of the previous view我使用NavigationLink
从“View1”导航到“View2”,在第二个视图上,后退按钮获取上一个视图的标题
But, if the title of the previous view is very long, then the back button gets the text "Back"但是,如果前一个视图的标题很长,则后退按钮将获取文本“后退”
How could I change that "Back" text?我怎么能改变那个“返回”文本?
I wanna make my app available in multiple languages, but it seems that "Back" does not change when phone's language changes我想让我的应用程序以多种语言提供,但是当手机的语言改变时,“返回”似乎没有改变
struct ContentView: View {
var body: some View {
return NavigationView {
VStack {
Text("View1")
NavigationLink(destination: Text("View2").navigationBarTitle("Title View2", displayMode: .inline)) {
Text("NavigationLink")
}
}.navigationBarTitle("Title View1")
}
}
}
PS: I'd like to keep this functionality as it it, I just want to change the language used for back button PS:我想保留这个功能,我只想更改用于后退按钮的语言
here is a workaround ....这是一个解决方法......
struct ContentView: View {
@State private var isActive: Bool = false
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView(), isActive: $isActive) {
Text("Title View2")
}
}.navigationBarTitle(! isActive ? "Title View2" : "Your desired back Title", displayMode: .inline)
}
}
}
struct DetailView: View {
var body: some View {
Text("View2")
}
}
You can create a custom back button in your navigation link by hiding native navigationBackButton.您可以通过隐藏本机 navigationBackButton 在导航链接中创建自定义后退按钮。 In the custom back button, you can add your translated custom back button title.在自定义后退按钮中,您可以添加已翻译的自定义后退按钮标题。
struct ContentView: View {
var body: some View {
return NavigationView {
VStack {
Text("View1")
NavigationLink("NavigationLink", destination: NextView())
}.navigationBarTitle("Title View1")
}
}
}
struct NextView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var backButton : some View { Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack {
Image("backImage") // BackButton Image
.aspectRatio(contentMode: .fit)
.foregroundColor(.white)
Text("Go Back") //translated Back button title
}
}
}
var body: some View {
VStack {
Text("View2")
}
.navigationBarTitle("Title View2",displayMode: .inline)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: backButton)
}
}
Output:-输出:-
I've managed to localize back buttons by providing translations for the Back
key in the Localizable.strings
file.通过为Localizable.strings
文件中的Back
键提供翻译,我设法本地化了后退按钮。
I am using SwiftUI though.不过我正在使用 SwiftUI。
Create your own button, then assign it using .navigationBarItems().创建您自己的按钮,然后使用 .navigationBarItems() 分配它。 I found the following format most nearly approximated the default back button.我发现以下格式最接近默认的后退按钮。
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
var backButton : some View {
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
HStack(spacing: 0) {
Image(systemName: "chevron.left")
.font(.title2)
Text("Cancel")
}
}
}
Make sure you use .navigationBarBackButtonHidden(true)
to hide the default button and replace it with your own!确保您使用.navigationBarBackButtonHidden(true)
隐藏默认按钮并将其替换为您自己的!
List(series, id:\.self, selection: $selection) { series in
Text(series.SeriesLabel)
}
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: backButton)
Add localization to your project.将本地化添加到您的项目中。 If language was set with user device settings(or simulator), after you add localization to your project it will work.如果语言是使用用户设备设置(或模拟器)设置的,在您向项目添加本地化后,它将起作用。 Project's supported language must match with selected one on device.项目支持的语言必须与设备上选择的语言相匹配。
I found a solution which can work also very good.我找到了一个也可以很好地工作的解决方案。
View1 set a toolbar item with .principal and add your Text or what you want. View1 使用 .principal 设置一个工具栏项并添加您的文本或您想要的内容。
for example :例如 :
ToolbarItem(placement: .principal) {
HStack{
Text("View1")
}.font(.subheadline)
}
and set also your title in View1:并在 View1 中设置您的标题:
.navigationTitle("Back")
And do nothing in your view2.并且在您的视图中什么都不做。 it will automatically add your view1 title to your view2 default back button text它会自动将您的 view1 标题添加到您的 view2 默认后退按钮文本中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.