【问题标题】:why there is an unexpected space when jumping to another page in swift为什么快速跳转到另一个页面时会出现意外空间
【发布时间】:2021-11-19 09:32:30
【问题描述】:

我想使用 NavigationLink 跳转到另一个页面,但是如下图所示,屏幕顶部出现了意外的空间。这以前没有发生过。我不知道是因为我的首页还是我试图跳转到的页面。

起始页


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

【问题讨论】:

  • 从主页中移除 NavigationView。

标签: ios swift xcode swiftui navigationview


【解决方案1】:

您所做的是,您将 2 个NavigationViews 堆叠在一起。 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
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多