swiftscrollviewoverscroll

Disable overscroll in ScrollView (SwiftUI)


How can I disable overscroll in fullscreen vertical ScrollView (SwiftUI)?

https://www.youtube.com/watch?v=dOyWCDhlxv8 - example of overscroll (I know, on video old Android device)

 var body: some View {
    GeometryReader { geometry in
        ScrollView([.vertical], showsIndicators: false)
        {
            VStack
                {
                    VStack (alignment: .leading) {

                        ForEach (0..<leftmenuuser.menuitems.count)
                        { i in
                            if (leftmenuuser.menuitems[i].index >= 0)
                            {
                                HStack {
                                    HStack (spacing: leftmenuuser.self.iconssize)
                                    {
                                        Image(leftmenuuser.menuitems[i].imagename).resizable().frame(width: leftmenuuser.self.iconssize, height: leftmenuuser.self.iconssize)

                                        Text(leftmenuuser.menuitems[i].title).foregroundColor(Color.white).font(.custom("PFDinTextCompPro-Regular", size: leftmenuuser.self.smallfontsize)).lineLimit(1)

                                        Spacer()
                                    }.padding(leftmenuuser.self.mypadding).frame(maxWidth: .infinity)
                                }.background(self.selectedindex == leftmenuuser.menuitems[i].index ? Color.yellow : Color.white).onTapGesture {
                                    leftmenuuser.menuitems[i].action(leftmenuuser.menuitems[i])
                                    self.selectedindex = leftmenuuser.menuitems[i].index
                                    self.showmenu = false
                                }
                            }
                        }
                    }
                    Spacer()
            }
            .background(Color.blue).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
            }.background(Color.red).edgesIgnoringSafeArea(.all)
    }
}

Solution

  • You can turn off bouncing for that specific view:

     var body: some View {
      init() {
        UIScrollView.appearance().bounces = false
      }
        GeometryReader { geometry in
            ScrollView([.vertical], showsIndicators: false)
            {
                VStack
                    {
                        VStack (alignment: .leading) {
    
                            ForEach (0..<leftmenuuser.menuitems.count)
                            { i in
                                if (leftmenuuser.menuitems[i].index >= 0)
                                {
                                    HStack {
                                        HStack (spacing: leftmenuuser.self.iconssize)
                                        {
                                            Image(leftmenuuser.menuitems[i].imagename).resizable().frame(width: leftmenuuser.self.iconssize, height: leftmenuuser.self.iconssize)
    
                                            Text(leftmenuuser.menuitems[i].title).foregroundColor(Color.white).font(.custom("PFDinTextCompPro-Regular", size: leftmenuuser.self.smallfontsize)).lineLimit(1)
    
                                            Spacer()
                                        }.padding(leftmenuuser.self.mypadding).frame(maxWidth: .infinity)
                                    }.background(self.selectedindex == leftmenuuser.menuitems[i].index ? Color.yellow : Color.white).onTapGesture {
                                        leftmenuuser.menuitems[i].action(leftmenuuser.menuitems[i])
                                        self.selectedindex = leftmenuuser.menuitems[i].index
                                        self.showmenu = false
                                    }
                                }
                            }
                        }
                        Spacer()
                }
                .background(Color.blue).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
                }.background(Color.red).edgesIgnoringSafeArea(.all)
        }
    }
    

    Or for all scroll views in the entire app by adding this to your AppDelegate:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UIScrollView.appearance().bounces = false
    }