为什么在 swift 中跳转到另一页时出现意外 space

why there is an unexpected space when jumping to another page in swift

我想使用 NavigationLink 跳转到另一个页面,但是在屏幕顶部有一个意想不到的 space,如下图所示。这在以前没有发生过。不知道是我的首页还是我尝试跳转的页面。

起始页


import SwiftUI

struct HomePage: View {

    var body: some View {
        NavigationView{
            VStack {
                
                NavigationLink(destination: chartDiretor()) {
                       Text("clike")
                    
                }
                NavigationLink(destination: chartDiretor()) {
                       Text("clike")
                    
                }
            }
        }

    }
}

目标页面


import SwiftUI

struct SwiftUIView: View {
    var body: some View {
        NavigationView{
            List{
                Text("something")
            }
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}




when clike the navigationLink. space highlighted with red rectangle line

what the the page I try to jump to looks like

您所做的是,您将 2 个 NavigationView 堆叠在一起。 SwiftUIView 中的一个再次嵌入 HomePage 中的另一个 NavigationView。您当前的视图层次结构如下所示,

NavigationView // First NavigationView
|- SwiftUIView
   |- NavigationView // Second NavigationView
      |- Some Content    

当它实际应该看起来像时,

NavigationView // First and ONLY NavigationView
|- SwiftUIView
   |- Some Content    

因此,要解决此问题,您可以删除 SwiftUIView 中的 NavigationView,如下所示,

struct SwiftUIView: View {
    var body: some View {
        List{
            Text("something")
        }
        .navigationBarTitle("Country") // Set the Navigation title here
    }
}