admin管理员组

文章数量:1296912

I want a view to have a navigation title, but I don't want the title to show on the screen; so that when I long press on the back button after showing a couple of views, I would see the title, not "Back"

I tried to add

.navigationTitle(movie.getMovieTitle())
.toolbar(.hidden, for: .navigationBar)
.navigationBarBackButtonHidden(false)

but it didn't work

I want a view to have a navigation title, but I don't want the title to show on the screen; so that when I long press on the back button after showing a couple of views, I would see the title, not "Back"

I tried to add

.navigationTitle(movie.getMovieTitle())
.toolbar(.hidden, for: .navigationBar)
.navigationBarBackButtonHidden(false)

but it didn't work

Share asked Feb 11 at 17:04 Mark GeeMark Gee 3491 gold badge2 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If I understand correctly, the back button should be visible on nested screens, but the navigation titles should always be hidden.

One way to achieve this is to set the navigationBarTitleDisplayMode to .inline, then supply your own ToolbarItem with placement .principal. The item you supply takes the place of the default navigation title. The replacement can just be some dummy text that is hidden:

struct ContentView: View {

    private var view1: some View {
        VStack {
            NavigationLink("Go to View 2") {
                Text("View 2")
                    .navigationBarBackButtonHidden(false)
            }
            .navigationTitle("View 1")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .principal) {
                    Text("").hidden()
                }
            }
        }
    }

    var body: some View {
        NavigationStack {
            VStack {
                NavigationLink("Go to View 1") {
                    view1
                }
            }
            .navigationTitle("Home")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .principal) {
                    Text("").hidden()
                }
            }
        }
    }
}

本文标签: iosHide navigation bar without losing its features Swift UIStack Overflow