【问题标题】:In my NavigationView '.edgesIgnoringSafeArea' does not move content past the safe area在我的 NavigationView '.edgesIgnoringSafeArea' 中不会将内容移动到安全区域之外
【发布时间】:2020-02-09 13:03:06
【问题描述】:

我希望 .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!
    }
}


【问题讨论】:

  • 您必须发布一些代码,以便我们可以看到您可能做错了什么。
  • 在您发布时正在编辑,已完成

标签: swiftui


【解决方案1】:

所以基本上,这是你的观点(提示,总是尝试发布人们可以复制并粘贴到 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) 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)
  }
}

【讨论】:

  • 这正是我想要的。谢谢!将在船上收取小费
猜你喜欢
  • 1970-01-01
  • 2020-12-15
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-25
  • 2016-12-13
  • 2022-10-18
相关资源
最近更新 更多