Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationView adding top padding

I have a very simple view, a background color and some text. But when I wrap it in a NavigationView I'm getting unwanted spacing at the top of the view. How can I eliminate this? (I want the Navigation as I will be adding NavigationLinks)

e.g.

var body: some View {
    NavigationView {
        ZStack(alignment: .leading) {
            Rectangle()
                .fill(
                    LinearGradient(
                        gradient: Gradient(colors: [.indigo, .purple]),
                        startPoint: .bottom,
                        endPoint: .top
                    )
                )
                .ignoresSafeArea()
            
            VStack {
                Text("Test")
                Spacer()
            }
        }
    }
}

enter image description here

like image 884
FlatDog Avatar asked Apr 26 '26 16:04

FlatDog


2 Answers

You can fix this by using .navigationBarHidden(true).

However, .navigationBarHidden(true) will be deprecated in the new version of iOS, so you can use .toolbar(.hidden) instead.

like image 151
Steven-Carrot Avatar answered Apr 29 '26 22:04

Steven-Carrot


The space is reserved for the title, so if you set the navigation bar title display mode to .inline, then it should reduce that space.

NavigationView {
    ZStack {
        /* ... */
    }
    .navigationBarTitleDisplayMode(.inline)
}
like image 23
ProgrammerG Avatar answered Apr 29 '26 22:04

ProgrammerG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!