26

Scroll View

I have a UIScrollView, with constraints left: 0, top: 0, right: 0, bottom: 0

Inside Scroll View

At the top of this UIScrollView is a UIImageView with constraints left: 0, top: 0, right: 0, height: 200

Underneath this I have a UITextView with constraints left: 0, top: 0, right: 0, bottom: 0

This means the UITextView will resize with respect to its content, and I set the scrollingEnabled to false for the UITextView.

So, when I run, it almost works perfectly.

The one problem is the UIImageView takes up about 10% more than the actual screen width. Hence, horizontal scrolling is enabled.

I have tried adding the lines

imageView.frame = CGRect(0, 0, screenSize.width, 200)
scrlView.contentSize.width = screenSize.width

but this makes no difference. I can still scroll horizontally and the Image View still takes up around 10% more than the actual screen width.

Note, I have not set imageView screen width in storyboard, only programatically.

Any ideas?

Greg Peckory
  • 7,700
  • 21
  • 67
  • 114
  • 1
    Do this `scrlView.contentSize.width = 0;` – iphonic Aug 25 '15 at 07:14
  • See this link http://stackoverflow.com/questions/27326924/swift-uiscrollview-correct-implementation-for-only-vertical-scrolling – Hardik Shekhat Aug 25 '15 at 07:37
  • I met same issue many times in the past. I solved my problem with using contentview in scrollView. I advice use uiview as contentview in scrollView. The fundamental point is width of contentView should be equal scrollView's width. All your imageview textview etc must be in content view. – muhammedkasva Aug 25 '15 at 08:07
  • 1
    Normally the scrollview won't scroll if the `contentsize.width <= width` of the scrollview, so if you don't want to do any calculation, safely set the `contentsize.width = 0;` that will eliminate the scroll for sure. – iphonic Aug 25 '15 at 08:11

6 Answers6

32

Swift 4

Horizontal Scroll Lock

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
        scrollView.contentOffset.x = 0
    }
}

You can change the x to y for vertical scrolling.

Make sure to add UIScrollViewDelegate like this:

class MyViewController: UIViewController, UIScrollViewDelegate {

    @IBOutlet var scrollView: UIScrollView!

    ...
}

And set the delegate for the ScrollView

scrollView.delegate = self
Hunter Copp
  • 539
  • 7
  • 14
30

Like this,

Swift 4.0

func scrollViewDidScroll(scrollView: UIScrollView) {
    if scrollView.contentOffset.x>0 {
        scrollView.contentOffset.x = 0
    }
}

And, you can set this property:

scrollImg.isDirectionalLockEnabled = true
Krunal Nagvadia
  • 1,083
  • 2
  • 12
  • 33
Larry Pickles
  • 4,615
  • 2
  • 19
  • 36
28

I changed this so that it just returns 0. No need to check at all if you want scroll off.

func scrollViewDidScroll(scrollView: UIScrollView) {
    scrollView.contentOffset.x = 0
}

No need for the directional lock.

the_dude_abides
  • 563
  • 4
  • 10
4
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollView.contentOffset.x = 0
}

This stops the scrollview from scrolling towards the leading edge too.

Steven Lee
  • 41
  • 2
1

If you are using storyboard and autolayout, then you should consider how ScrollView work in Storyboard with autolayout.

Consider the following.

  1. Add a single view on your scrollView with constrains left, right, top, bottom, height, width,

  2. Make the outlet of the width and height

  3. Add your subViews to the view you added in the scrollView

update the width to the screenSize eg: 320 for iPhone5 or 4.

self.viewWidth = SCREEN_WIDTH;
[self.view updateConstraints];
Rohit Kumar
  • 877
  • 6
  • 20
1

You can use

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollView.contentOffset.x = 0
}

Do not use scrollView.contentOffset.x < 0 or > 0 like others have mentioned because it will only stop scroll to one side horizontally and not both.

Shubham Goel
  • 1,962
  • 17
  • 25