How to remove the default Navigation Bar space in SwiftUI NavigiationView

后端 未结 15 2759
我在风中等你
我在风中等你 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:04

    This is a bug present in SwiftUI (still as of Xcode 11.2.1). I wrote a ViewModifier to fix this, based on code from the existing answers:

    public struct NavigationBarHider: ViewModifier {
        @State var isHidden: Bool = false
    
        public func body(content: Content) -> some View {
            content
                .navigationBarTitle("")
                .navigationBarHidden(isHidden)
                .onAppear { self.isHidden = true }
        }
    }
    
    extension View {
        public func hideNavigationBar() -> some View {
            modifier(NavigationBarHider())
        }
    }
    

提交回复
热议问题