swiftTonio
swiftTonio

Reputation: 119

Set UIScrollview Scroll Direction

I set the content size so vertical scrolling becomes activated but I only want the user to be able to scroll north and not south. Any ideas on how I can accomplish this?

//set high greater than view
myScroll.contentSize = CGSize(width: myView.view.frame.width,height: myView.view.frame.height + 190)

I only want the ability to scroll up and disable the ability to scroll down which is the default direction of the scrollview

Upvotes: 5

Views: 12460

Answers (4)

iWheelBuy
iWheelBuy

Reputation: 5679

You can set the direction of UIPanGestureRecognizer which is attached to your UIScrollView.

Check out some popular question which has an up-to-date answer, for example this one.

Or just go with pod 'UIPanGestureRecognizerDirection'

Upvotes: 0

rob mayoff
rob mayoff

Reputation: 385560

Make a subclass of UIScrollView. Override layoutSubviews to make sure contentOffset.y only increases, except when it's beyond the end of the content range, so the scroll view can bounce at the bottom.

class MyScrollView: UIScrollView {
    var priorOffset = CGPoint.zero

    override func layoutSubviews() {
        var offset = contentOffset

        if offset.y < priorOffset.y {
            let yMax = contentSize.height - bounds.height
            if offset.y < yMax {
                offset.y = priorOffset.y
                contentOffset = offset
            }
        }
        priorOffset = offset

        super.layoutSubviews()
    }
}

Result:

one-way scroll view

Upvotes: 1

Maulik shah
Maulik shah

Reputation: 1704

  1. select your Scrollview
  2. select identity inspector
  3. set user define attributes (see image)

    in image first 435 vertical scrolling & second 116 is horizontal scroll

Note : set your own scrolling

enter image description here

Upvotes: 0

Varun
Varun

Reputation: 759

You can set the content offset of the scroll view to the bottom. i.e.,

myScroll.contentOffset = CGPoint(x: 0,y: 190) // Here 190 is the same value that represent the height increase done in contentSize.

Upvotes: 3

Related Questions