Search code examples
iosswiftuinavigationview

SwiftUI NavigationView: Keeping back button while removing whitespace


Screen shot of white space I want to remove the empty space below the <Back button in the second navigation view. I know that this question has been asked several times before, but I have not been able to find a good solution that does this.

I have tried

.navigationBarTitle("")
.navigationBarHidden(true)

and

.navigationBarTitle("", displayMode: .inline)

without the desired result.

Any hints that could help me?

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
    }
}

struct FirstNavView: View {
    let listItems = ["One", "Two", "Three"]
    var body: some View {
        NavigationView {
            List(listItems, id: \.self) { item in
                NavigationLink(destination: SecondNavView(item: item)) {
                    Text(item).font(.headline)
                }
            }
        }
    }
}

Solution

  • It seens like your parent View hasn't a title, to solve this you need to set .navigationTitle inside NavigationView on parent View like this:

    NavigationView {
        
        VStack {
            //....
        }
        
        .navigationTitle(Text("Awesome View"))
        .toolbar {
            ToolbarItem(placement: .principal){
                // Put any view (Text, Image, Stack...) you want here
            }
        }
    }