UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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

2      

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.

2      

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

2      

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

2      

@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!

2      

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.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.