SwiftUI NavigationView remove white space

Viewed 2305

I have a HomeView

NavigationView {
    ZStack {
        VStack {
             NavigationLink(destination: ProfileView()) {
                 if session.userInSession?.activated != 1 {
                     completionText(message: "Complete Your Profile")
                 } else {
                     completionText(message: "Edit Your Profile")
                 }
             }
        }
    }
}

My ProfileView is not wrapped in a navigation view nor has a bar title:

VStack {
    ScrollView(showsIndicators: false) {
        ...
    }
}

But my ProfileView is also accessible from my SettingView

NavigationView {
    VStack(alignment: .leading) {
        List {
            NavigationLink(destination: ProfileView()) {
            }
        }
    }
}

When I access my ProfileView from the setting screen, it displays fine. But when I access it from my HomeView it creates white space at the top:

enter image description here

When I go through Settings its fine:

enter image description here

How can I remove this white space above?

2 Answers

It looks like your HomeView/s NavigationView has .navigationBarTitleDisplayMode(.large) or automatic, which is by default .large, but your SettingView has .navigationBarTitleDisplayMode(.inline) (taking into account divider below < Setting), so you see different height of title bars.

Of course it is assumption due not absent of detailed code, but possible solution to make it same would be add explicit mode for ProfileView, eg:

VStack {
   ScrollView(showsIndicators: false) {
   }
}.navigationBarTitleDisplayMode(.inline)     // << here !!

This space is probably an empty title of your NavigationView.

You can try hiding your navigation bar title:

NavigationView {
    ZStack {
        ...
    }
    .navigationBarTitle("")
    .navigationBarHidden(true)
}

As a test try setting .navigationBarTitle("Some title") and see if you still see this empty space or if it's replaced by Some title text.

Related