0

I have a simple view hierarchy between two views in SwiftUI.

MainView: This is the top level screen

import SwiftUI

struct MainView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SearchView()) {
                    Text("Search")
                }
            }
            .navigationBarTitle("")
            .navigationBarHidden(true)
        }
    }
}

SearchView:

import SwiftUI

struct SearchView: View {
    var body: some View {
        Text("Hello, World!")
    }
}

I don't want a navigation bar on my main view, but I do want a "back" button on the Search view. This is how the Search view looks:

enter image description here

I don't want the space above the back button - where is that coming from?

I inspected the view hierarchy and it seems the view has TWO UINavigationBar objects. If I hide the navigation bar inside the Search View, the back button goes but the space still remains.

How do I get rid of this unwanted space above the Back button?

Bonton255
  • 2,231
  • 3
  • 28
  • 44
  • Not reproducible for me, loading `MainView` as the top-level view on iOS 14.3. Do you have anything above it in the view hierarchy? – jnpdx Jan 31 '21 at 06:19
  • Sorry! I figured it out. I had a `NavigationView` on the App as well. Removing the `NavigationView` from the `MainView` solved the problem. Thanks for responding anyway. Is it preferable to have the `NavigationView` in the `App` or the `MainView`? – Bonton255 Jan 31 '21 at 06:24
  • That's your decision based on the architecture of your app – jnpdx Jan 31 '21 at 06:26
  • Does this answer your question? [How to get rid of space in nested NavigationView with SwiftUI](https://stackoverflow.com/questions/62722769/how-to-get-rid-of-space-in-nested-navigationview-with-swiftui) – pawello2222 Feb 01 '21 at 15:05

0 Answers0