【问题标题】:How to fill the background of the top most view on scroll using ScrollView in SwiftUI?如何在 SwiftUI 中使用 ScrollView 填充滚动时最顶部视图的背景?
【发布时间】:2022-11-08 14:37:26
【问题描述】:

我在ScrollView 的最顶部有一个背景颜色为绿色的视图:

我想保持弹跳效果,但是当我向下滚动(向上)时,它的白色背景颜色如下:

我想让它保持相同的颜色,这样它就不会在我滚动时显示背景颜色(在这种情况下,它是白色的)。我如何实现这一目标?到目前为止,这是我的代码:

NavigationView {
    GeometryReader { proxy in
        ScrollView(showsIndicators: false) {
            VStack(spacing: 19) {
                VStack { // This is the view with the green background
                    // some code goes here
                }.frame(height: proxy.safeAreaInsets.top + 171, alignment: .top)
                .frame(minWidth: 0, maxWidth: .infinity)
                .background(Color.PRIMARY_BACKGROUND)
            
                // some code goes here
            }
        }
    }

}

【问题讨论】:

    标签: swiftui


    【解决方案1】:

    在 NavigationView() 之后添加一个 Zstack() 并给它一个背景颜色。

    NavigationView {
        ZStack{
            GeometryReader { proxy in
                ScrollView(showsIndicators: false) {
                    VStack(spacing: 19) {
                        VStack { // This is the view with the green background
                            // some code goes here
                        }.frame(height: proxy.safeAreaInsets.top + 171, alignment: .top)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .background(Color.PRIMARY_BACKGROUND)
    
                        // some code goes here
                    }
                }
            }
        }
        .background(Color.PRIMARY_BACKGROUND)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多