在 SwiftUI 中删除或更改视图底部安全区域的颜色

Remove, or change color of the safe area at the bottom of the View in SwiftUI

这是屏幕截图:

查看代码如下:

struct ViewDetails: View {

    @EnvironmentObject var displayDetails: DisplayDetails

    var body: some View { 

        ScrollView {

            GeometryReader { geometry in

                ZStack {

                    if geometry.frame(in: .global).minY <= 0 {

                        Image("header")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: geometry.size.width, height: geometry.size.height)
                            .offset(y: geometry.frame(in: .global).minY/9)
                            .clipped()

                    } else {

                        Image("header")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: geometry.size.width, height: geometry.size.height + geometry.frame(in: .global).minY)
                            .clipped()
                            .offset(y: -geometry.frame(in: .global).minY)

                     }

                }   

            }.frame(height: 400)

            VStack(alignment: .leading) {

                HStack {

                    Image("author")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 60, height: 60)
                        .clipped()
                        .cornerRadius(10)

                    VStack(alignment: .leading) {

                        Text("Article by")
                            .font(.custom("AvenirNext-Regular", size: 15))
                            .foregroundColor(.gray)

                        Text("John Doe")
                            .font(.custom("AvenirNext-Demibold", size: 15))

                    }

                }.padding(.top, 20)

                Text("Lorem ipsum dolor sit amet")
                    .font(.custom("AvenirNext-Bold", size: 30))
                    .lineLimit(nil)
                    .padding(.top, 10)

                Text("3 min read • 22. November 2019")
                    .font(.custom("AvenirNext-Regular", size: 15))
                    .foregroundColor(.gray)
                    .padding(.top, 10)

                Text(articleContent)
                    .font(.custom("AvenirNext-Regular", size: 20))
                    .lineLimit(nil)
                    .padding(.top, 30)

            }
            .frame(width: 350)

        }
        .edgesIgnoringSafeArea(.all)

            .onAppear(perform: {

                self.displayDetails.showFullScreen.toggle()

            })

    }

}

所以我不确定我是否可以删除底部显示为 gray/white 的任何内容,或者我是否可以更改它的颜色,这两种方法都适合我,但我无法实现! ?谁能帮忙

我稍微更改了您的代码并 运行 它在模拟器上。唯一的变化是在 .edgesIgnoringSafeArea(.all) 之前添加 .background(Color.yellow)。这是带有结果的完整代码片段:

struct ViewDetails: View {

    var body: some View {

        ScrollView {

            GeometryReader { geometry in

                ZStack {

                    if geometry.frame(in: .global).minY <= 0 {

                        Image("hp")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: geometry.size.width, height: geometry.size.height)
                            .offset(y: geometry.frame(in: .global).minY/9)
                            .clipped()

                    } else {

                        Image("header")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: geometry.size.width, height: geometry.size.height + geometry.frame(in: .global).minY)
                            .clipped()
                            .offset(y: -geometry.frame(in: .global).minY)

                     }

                }

            }.frame(height: 400)


            VStack(alignment: .leading) {

                HStack {

                    Image("author")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 60, height: 60)
                        .clipped()
                        .cornerRadius(10)

                    VStack(alignment: .leading) {

                        Text("Article by")
//                            .font(.custom("AvenirNext-Regular", size: 15))
                            .foregroundColor(.gray)

                        Text("John Doe")
                            .font(.custom("AvenirNext-Demibold", size: 15))

                    }

                }.padding(.top, 20)

                Text("Lorem ipsum dolor sit amet")
//                    .font(.custom("AvenirNext-Bold", size: 30))
                    .lineLimit(nil)
                    .padding(.top, 10)

                Text("3 min read • 22. November 2019")
                    .font(.custom("AvenirNext-Regular", size: 15))
                    .foregroundColor(.gray)
                    .padding(.top, 10)

                Text("articleContent")
                    .font(.custom("AvenirNext-Regular", size: 20))
                    .lineLimit(nil)
                    .padding(.top, 30)

            }
            .frame(width: 350)


        }
        .background(Color.yellow)
            .onAppear(perform: {

            //                self.displayDetails.showFullScreen.toggle()

                        })
        .edgesIgnoringSafeArea(.all)   

    }

结果:

P.S: 你的代码片段中有很多不必要的细节,很难分析=(