What is the best way to disable horizontal scroll of UIScrollView?

10.6k Views Asked by At

In UIScrollView, I have content height and width greater than scrollview's size. So basically it can scroll horizontally and vertically. What I want is to scroll vertically only and I manage horizontally scroll using an UIButton by using below code.

 [scrlViewQuestion setContentOffset:CGPointMake(questionWidth, 0) animated:YES];

Preventing horizontal scrollview, one can just set scrollview content width lesser than scrollview size but in my case scrollview content width is greater than its size? So what is the best way to solve this?

6

There are 6 best solutions below

1
mschmidt On

Set the contentSize of your scrollview to the width of your screen and the actual height of your scrollable content. Set the contentInset to zero for the top, bottom, and right. For the content inset on the left always choose the negated x coordinate of the position you currently want to display.

E.g. if your screen is 320 points wide, and you want to display content with horizontal scrolling fixed at x, but vertical scrolling allowed:

CGFloat screenWidth = 320.0;
CGSize actualContentSize = CGSizeMake(3000, 3000);

[scrlViewQuestion setContentSize:CGSizeMake(screenWidth, actualContentSize.height)];
[scrlViewQuestion setContentInset:UIEdgeInsetsMake(0, -x, 0, 0)];
1
Harminder Singh On

You can do this in storyboard.

enter image description here

Disable the Show Horizontal Indicator tab from the Scroll View's Identity Inspector.It will disable your horizontal scrolling.

0
ManjunathK On

Content UIView width should be equal to the width of UIScrollView's superview for instance, not UIScrollView itself.

enter image description here

0
Ryan110 On

swift 4. set the delegate

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

Using this view hierarchy and these constraints

enter image description here


Step by Step explanation of the image

Place your ScrollView inside of a view(OuterView).

Place the view with your content(InnerView) inside the ScrollView

Align the ScrollViews 4 sides to the OuterViews 4 sides

ScrollView.leading = OuterView.leading
ScrollView.trailing = OuterView.trailing
ScrollView.top = OuterView.top
ScrollView.bottom = OuterView.bottom

Align the InnerViews 4 sides to the ScrollViews 4 sides

InnerView.leading = ScrollView.leading
InnerView.trailing = ScrollView.trailing
InnerView.top = ScrollView.top
InnerView.bottom = ScrollView.bottom

Set the InnerViews width and height equal to the OuterViews width, NOT the ScrollViews width.

InnerView.width = OuterView.width

Source

0
mohsen On

SWIFT 5 first, you should create a delegation class or extend the view control with UIScrollViewDelegate, and after, you should check the content offset with scrollViewDidScroll(_:) func and disable it with that: here is a sample code:

class ViewController: UIViewController {
  @IBOutlet weak var scrollView: UIScrollView!
    .
    .
    .
 override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.delegate = self 
 }
}

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