SwiftUI UINavigationBar 不会忽略顶部安全区域。如何摆脱顶部的空白空间?

时间:2021-06-11 18:13:20

标签: swiftui uinavigationbar swiftui-navigationview uinavigationbarappearance

enter image description here

我用来创建此导航栏示例的代码:

struct ContentView: View {
    init() {
        let defaultAppearance = UINavigationBarAppearance()
        defaultAppearance.configureWithOpaqueBackground()
        defaultAppearance.backgroundColor = .red

        UINavigationBar.appearance().standardAppearance = defaultAppearance
        UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    }

    var body: some View {
        ParentView()
    }
}

struct ParentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: DetailView()) {
                    Text("Tap here")
                }
            }
            .navigationBarTitle("", displayMode: .inline)
            .navigationBarHidden(true)
            .edgesIgnoringSafeArea(.all)
        }
    }
}

struct DetailView: View {
    var body: some View {
        VStack {
            Text("Detail View")
        }
        .edgesIgnoringSafeArea(.all)
   }
}

我把 .edgesIgnoringSafeArea(.all) 放在哪里并不重要,它不起作用。有没有其他方法可以告诉导航栏忽略安全区域?

如果不是安全区域问题,那么我需要使整个导航栏与 UI Hierarchy 中显示的 UINavigationBarContentView 具有相同的高度: enter image description here

0 个答案:

没有答案