在我的 NavigationView '.edgesIgnoringSafeArea' 中,内容不会移动到安全区域之外

rap*_*711 0 swiftui

我希望 .edgesIgnoringSafeArea(.all) 将内容适合到画布的顶部

我希望它显示如下: https: //i.stack.imgur.com/RzrO6.png

相反,它显示如下: https: //i.stack.imgur.com/sbbaU.png

内容查看:

TabView {
    NavigationView {
        MainContentView()
    }
...
}
Run Code Online (Sandbox Code Playgroud)

主要内容查看:

var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            ...
        }
        .padding([.horizontal, .top])
        .navigationBarTitle("Title")
    }
Run Code Online (Sandbox Code Playgroud)

详细查看:

struct PostDetail: View {    
    var post: Post

    var body: some View {
        List {
            Image(post.imageName)
            .resizable()
            .aspectRatio(contentMode: .fit)
            .listRowInsets(EdgeInsets())
            VStack(spacing: 20) {
                Text(post.name)
                    .foregroundColor(.primary)
                    .font(.largeTitle)
                    .fontWeight(.bold)
                Text(post.description)
                    .foregroundColor(.primary)
                    .font(.body)
                    .lineLimit(nil)
                    .lineSpacing(12)
                VStack {
                    HStack {
                        Spacer()
                        ViewMoreButton(post: post)
                        Spacer()
                    }
                }
            }
            .padding(.top)
            .padding(.bottom)
        }
        .edgesIgnoringSafeArea(.top) //Does nothing!
        .navigationBarHidden(true) // Does nothing!
    }
}


Run Code Online (Sandbox Code Playgroud)

Kev*_*ers 6

基本上,这就是您的观点(提示,始终尝试发布人们可以复制并粘贴到 Xcode 中的最低版本):

struct ContentView: View {
  var body: some View {
    TabView {
      NavigationView {
        VStack(alignment: .leading, spacing: 20) {
          List {
            Image("bg")
              .resizable()
              .aspectRatio(contentMode: .fit)
              .listRowInsets(EdgeInsets())
          }
          .edgesIgnoringSafeArea(.top)
          .navigationBarHidden(true)
        }
        .navigationBarTitle("Title")
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

是的,我看到了同样的情况:图像没有显示在顶部安全区域中。一旦删除 TabView,它就会按预期工作。

修复方法是将.edgesIgnoringSafeArea(.top)ALSO 添加到TabView

struct ContentView: View {
  var body: some View {
    TabView {
      NavigationView {
        VStack(alignment: .leading, spacing: 20) {
          List {
            Image("bg")
              .resizable()
              .aspectRatio(contentMode: .fit)
              .listRowInsets(EdgeInsets())
          }
          .edgesIgnoringSafeArea(.top)
          .navigationBarHidden(true)
        }
        .navigationBarTitle("Title")
      }
    }
    .edgesIgnoringSafeArea(.top)
  }
}
Run Code Online (Sandbox Code Playgroud)