3

I've a scrollview and i want that scrollview to only scroll downwards and it should not scroll in any other direction. It is not about horizontal or vertical but that I want the scrollview to scroll only downwards and not upwards in vertical mode.

Khushal Dugar
  • 230
  • 4
  • 12
  • You need to set scrollView.contentSize larger than its frame size for scrolling. If you want horizontal scrolling, set contentSize's width larger than frame width. – nynohu May 12 '17 at 07:53
  • possible duplicate of http://stackoverflow.com/questions/5370428/uiscrollview-disable-scrolling-in-just-one-direction – user3581248 May 12 '17 at 08:24

1 Answers1

1

Solution

Subclass UIScrollView and override the methods to restrict horizontal scrolling and only scroll if direction is downwards:

class DownwardsOnlyScrollView: UIScrollView
{
    override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
        // restrict movement to vertical only
        let newOffset = CGPoint(x: 0, y: contentOffset.y)

        //only scroll if scroll direction is downwards
        if newOffset.y > self.contentOffset.y
        {
            super.setContentOffset(newOffset, animated: animated)
        }
    }
}
torinpitchers
  • 1,282
  • 7
  • 13