在我的NavigationView中,“。edgesIgnoringSafeArea”不会将内容移出安全区域

时间:2020-02-09 13:03:06

标签: swiftui

我希望它使.edgesIgnoringSafeArea(.all)内容恰好适合画布顶部

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

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

内容视图:

TabView {
    NavigationView {
        MainContentView()
    }
...
}

主要内容视图:

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

详细视图:

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!
    }
}


1 个答案:

答案 0 :(得分:2)

基本上,这就是您的观点(提示,请始终尝试发布一个人们可以复制并粘贴到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")
      }
    }
  }
}

是的,我看到的也一样:该图像未显示在顶部安全区域中。删除TabView后,它就会按预期工作。

解决方法是将.edgesIgnoringSafeArea(.top)也添加到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)
  }
}