Home > Net >  How can I prevent users from scrolling past a certain point?
How can I prevent users from scrolling past a certain point?

Time:10-18

I have a ScrollView, when I scroll up, the content on the screen would move down. But I wanna limit how far the content can move down. I don't want the user to be able to scroll beyond the gray rectangle at the top. In other words, I don't want the user to see the white area above the gray rectangle when they scroll up. How can I achieve this in SwiftUI?

enter image description here

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()
    }
}

CodePudding user response:

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()
}
}

CodePudding user response:

You can remove the 'bounce' effect from your scrollview, something like

UIScrollView.appearance().bounces = false
  • Related