NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: Removing unwanted and unknown whitespace (possibly a navigation bar?) above a view.

Forums > SwiftUI

I've got an issue with a white space that I'm not really sure what it is and why it's there, it might be somekind of navigation bar, but I can't affect it in that case...

In a view I have NavigationView with a list of NavigationLinks. In that View there is no navigation bar and no white space above it:

NavigationView{
            List(configurationList.configurations.filter{$0.fkTypeId == configurationList.currentProductType}, id: \.uid) { item in
                NavigationLink(destination: NewLogScanBarcodesIconView().environmentObject(item)){
                    Text(item.name)
                        .font(.headline)
                }
            }
            .navigationBarTitle("")
            .navigationBarBackButtonHidden(true)
            .navigationBarHidden(true)
        }

When I click the NavigationLink I come to the destination, but above that view (and alla following views) is a white space, similar to a navigation bar but I don't know why it's there and how to remove it. That view has no NavigationView at all, but it does have another NavigationLink. I've tried to make that view into a navigationView on its own (even if it doesn't really fit its purpose) to try to remove the white space by using the ".navigationBarHidden" on it as well, but that just gives me an extra white space that I can remove, but the original white space is still there.

So is it a navigationBar or anything else? How do I remove it?

   

You could try .navigationViewStyle(StackNavigationViewStyle()) above the .navigationBarTitle. That assuming the NewLogScanBarcodesIconView() does not have a NavigationView in it.

   

That view has no NavigationView at all

But if you got to it through a NavigationLink in a NavigationView, then it is already in a NavigationView. You need to set navigationBarHidden there as well.

   

But if you got to it through a NavigationLink in a NavigationView, then it is already in a NavigationView. You need to set navigationBarHidden there as well.

How do you set modifiers for a navigationView that already exists then? I've tried creating it again as a way to access it, but that just created a new one...

   

The same way you add them to any View in a NavigationView:

struct SecondNavView: View {
    let item: String

    var body: some View {
        ZStack {
            Color.red
            Text(item)
        }
        .navigationBarTitle("")
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}

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)
                }
            }
            .navigationBarTitle("")
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
        }
    }
}

   

@roosterboy That worked!

I'm still new to iOS programming and find the structure confusing sometimes. To me it would be logical to modify a views behavior by setting modifiers on the view itself, I forgot that you can do it by setting modifiers on a view inside the NavigationView.

Thanks for the help!

   

With NavigationView, it helps to realize it's really just a container for a stack of views. Each of those views in the stack can have a different navigation title, toolbar, buttons, etc. so it makes sense to set those on the contained views rather than on the container.

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.