How to remove the default Navigation Bar space in SwiftUI NavigiationView

后端 未结 15 2638
我在风中等你
我在风中等你 2020-11-29 20:00

I am new to SwiftUI (like most people) and trying to figure out how to remove some whitespace above a List that I embedded in a NavigationView

In this image, you can

相关标签:
15条回答
  • 2020-11-29 20:16

    For me, I was applying the .navigationBarTitle to the NavigationView and not to List was the culprit. This works for me on Xcode 11.2.1:

    struct ContentView: View {
        var body: some View {
            NavigationView {
                List {
                    NavigationLink(destination: DetailView()) {
                        Text("I'm a cell")
                    }
                }.navigationBarTitle("Title", displayMode: .inline)
            }
        }
    }
    

    0 讨论(0)
  • 2020-11-29 20:18

    You could extend native View protocol like this:

    extension View {
        func hideNavigationBar() -> some View {
            self
                .navigationBarTitle("", displayMode: .inline)
                .navigationBarHidden(true)
        }
    }
    

    Then just call e.g.:

    ZStack {
        *YOUR CONTENT*
    }
    .hideNavigationBar()
    
    0 讨论(0)
  • 2020-11-29 20:18

    If you set the title as inline for the View you want remove the space on, this doesn't need to be done on a view with a NavigationView, but the one navigated too.

    .navigationBarTitle("", displayMode: .inline)
    

    then simply change the Navigation bars appearance

    init() {
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
        UINavigationBar.appearance().shadowImage = UIImage()
    }
    

    on the view that holds the initial NavigationView.

    If you want to change the Appearance from screen to screen change the appearance in the appropriate views

    0 讨论(0)
提交回复
热议问题