BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

SOLVED: How do I get rid of the white region on top of a NavigationView

Forums > SwiftUI

@Apps  

I need to use a NavigationView because I need a search box there.

This is a sample code, very basic.

@State var searchText = ""

var body: some View {
    NavigationView {
      List {
        NavigationLink(destination: Color(.red) ) {
          Text("aaa")
        }
        NavigationLink(destination: Color(.red) ) {
          Text("bbb")
        }
        NavigationLink(destination: Color(.red) ) {
          Text("ccc")
        }
      }

    }
    .searchable(text: $searchText, prompt: "search...")
    .navigationBarTitle("", displayMode: .inline)
    .edgesIgnoringSafeArea(.top)

    .navigationBarHidden(true)
}

This is the result

Look at the annoying area at the top (I have colored it yellow on Photoshop for reference).

How do I get rid of this area and move the search box up?

   

I'm not sure but I think it has to do with

    .navigationBarTitle("", displayMode: .inline)

does it help to remove that line?

   

@Apps  

Nope, no difference at all.

   

@Apps  

I have discovered the problem. Just change the commands to the list...


@State var searchText = ""

var body: some View {
NavigationView {
List {
NavigationLink(destination: Color(.red) ) {
Text("aaa")
}
NavigationLink(destination: Color(.red) ) {
Text("bbb")
}
NavigationLink(destination: Color(.red) ) {
Text("ccc")
}
}
.searchable(text: $searchText, prompt: "search...")
.navigationBarTitle("", displayMode: .inline)
.navigationBarHidden(true)
}

}
.edgesIgnoringSafeArea(.top)
}

Thanks Apple.

   

@Bnerd  

if you enable .navigationBarHidden , there is no more search, so how this solved your problem?

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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.