In my NavigationView '.edgesIgnoringSafeArea' does not move content past the safe area

久未见 提交于 2021-02-11 14:00:16

问题


I would like it so that .edgesIgnoringSafeArea(.all) fits the content right onto the top of the canvas

I would like it to show like this: https://i.stack.imgur.com/RzrO6.png

Instead, it shows like this: https://i.stack.imgur.com/sbbaU.png

Content view:

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

Main content view:

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

Detailed view:

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:


So basically, this is your view (tip, always try to post a minimum version that people can just copy and paste into 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")
      }
    }
  }
}

And yeah, I see the same: the image is not shown in the top safe area. Once you remove the TabView, it does work as expected.

The fix is to add .edgesIgnoringSafeArea(.top) ALSO to the 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)
  }
}


来源:https://stackoverflow.com/questions/60136913/in-my-navigationview-edgesignoringsafearea-does-not-move-content-past-the-saf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!