frpa74
frpa74

Reputation: 37

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)
                }
            }
        }
    }
}

Upvotes: 0

Views: 1352

Answers (2)

Bruno Cavaletti Alves
Bruno Cavaletti Alves

Reputation: 64

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
        }
    }
}

Upvotes: 0

Asperi
Asperi

Reputation: 257573

I assume it is do to place of applied modifiers.

The following works (tested with Xcode 13.4 / iOS 15.5)

demo

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
        .navigationBarTitleDisplayMode(.inline)  // << here !!
    }
}

Upvotes: 1

Related Questions