如何从后退按钮中删除标题

How to remove the title from the back button

进入视图后,在 V 形图像旁边的后退按钮中,导航栏标题紧随其后。我怎样才能删除它但保持 navigationBarTittle?我试过 navigationBarBackButtonHidden 但没用。

更新

我补充了:

@State private var active: Bool = false

.navigationBarTitle(!active ? "Buscar" : "")

当我点击按钮时,另一个视图看起来像我想要的,但随后一切都变白了。

代码如下:

@EnvironmentObject var session: SessionStore
@Environment(\.colorScheme) var colorScheme

init() {
    UINavigationBar.appearance().backIndicatorImage = UIImage(systemName: "chevron.left")
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(systemName: "chevron.left")
}

@State private var active: Bool = false

var body: some View {
    NavigationView {
        VStack(spacing: 0) {
            ScrollView(.vertical, showsIndicators: false) {
                VStack(spacing: 18) {
                    ScrollView(.horizontal, showsIndicators: false) {
                        HStack(spacing: 20) {
                            Text("teste")
                                .frame(height: 180)
                                .frame(width: 330)
                                .background(Color.blue)
                                .cornerRadius(15)
                            Text("teste")
                                .frame(height: 180)
                                .frame(width: 330)
                                .background(Color.green)
                                .cornerRadius(15)
                            Text("teste")
                                .frame(height: 180)
                                .frame(width: 330)
                                .background(Color.pink)
                                .cornerRadius(15)

                        }.padding(.horizontal, 12)
                    }
                    ForEach(specialtyList, id: \.type) { specialty in

                        NavigationLink(destination: SearchBar(item: specialty), isActive: self.$active) {
                            VStack(spacing: 18) {
                                HStack {
                                    Text(specialty.type).foregroundColor(.white)
                                    specialty.image
                                        .renderingMode(.original)
                                        .resizable()
                                        .frame(width: 35, height: 35)

                                }.frame(minWidth: 0, idealWidth: 350, maxWidth: .infinity)
                                    .frame(height: 100)
                            }

                        }.padding(.horizontal)
                            .background(specialty.color).cornerRadius(45)

                    }.padding(.horizontal)
                }

                .padding(.top)
                .padding(.bottom)
            }

        }.navigationBarTitle(!active ? "Buscar" : "")

    }.accentColor(colorScheme == .dark ? Color.white : Color.black)
}

如果我对你的理解正确 child 在你想要的导航栏中查看:后退按钮(只有箭头,不是文本)和一些导航标题。

当您转到 child 视图时,您当前在 ContentView 中的内容会删除其标题:

.navigationBarTitle(!active ? "Buscar" : "")

因此 child 视图中的后退按钮将没有文本。

这意味着您可能也不需要此代码:

init() {
    UINavigationBar.appearance().backIndicatorImage = UIImage(systemName: "chevron.left")
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(systemName: "chevron.left")
}

此外,您需要在 child 视图中指定导航栏标题:

.navigationBarTitle("detail title")

您的代码不可重现,但这里有一个简单的演示:

struct ContentView: View {
    @State private var active: Bool = false

    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView(), isActive: self.$active) {
                    Text("go to...")
                }
            }
            .navigationBarTitle(!active ? "Buscar" : "")
        }
    }
}

struct DetailView: View {
    var body: some View {
        Text("detail view")
            .navigationBarTitle("detail title")
    }
}