首页 > 解决方案 > 如何防止用户滚动超过某个点?

问题描述

我有一个 ScrollView,当我向上滚动时,屏幕上的内容会向下移动。但我想限制内容可以向下移动的距离。我不希望用户能够滚动到顶部的灰色矩形之外。换句话说,我不希望用户在向上滚动时看到灰色矩形上方的白色区域。如何在 SwiftUI 中实现这一点?

在此处输入图像描述

import SwiftUI

struct ScrollViewIssue: View {
    var body: some View {
        ScrollView {
            VStack {
                Rectangle()
                    .foregroundColor(.gray)
                    .frame(height: 200)
                
                Circle()
                    .frame(width: 300, height: 300)
                
                Circle()
                    .frame(width: 300, height: 300)
                
                Circle()
                    .frame(width: 300, height: 300)
                
                Circle()
                    .frame(width: 300, height: 300)
            }
        }.ignoresSafeArea()
    }
}

标签: iosswiftswiftui

解决方案


struct ScrollViewIssue: View {
var body: some View {
    ScrollView {
        VStack {
            Rectangle()
                .foregroundColor(.gray)
                .frame(height: UIScreen.main.bounds.height)
                .frame(height: 200, alignment: .bottom)
            
            Circle()
                .frame(width: 300, height: 300)
            
            Circle()
                .frame(width: 300, height: 300)
            
            Circle()
                .frame(width: 300, height: 300)
            
            Circle()
                .frame(width: 300, height: 300)
        }
    }.ignoresSafeArea()
}
}

推荐阅读