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

6

8

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?

Wifeless answered 4/12, 2017 at 10:1 Comment(0)
Y
8

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

enter image description here

Yelmene answered 9/11, 2018 at 13:38 Comment(0)
F
2

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
        }
    }
    
}

Frication answered 3/11, 2020 at 19:18 Comment(0)
B
1

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

Borodino answered 8/9, 2019 at 20:51 Comment(0)
G
0

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)];
Gadgetry answered 4/12, 2017 at 18:49 Comment(1)
More precisely, set contentSize.width to the width of the scroll view (e.g. scrollView.bounds.size.width). However, if you are using layout constraints, autolayout will reset the scroll view's contentSize when it runs (which is probably more often than you realize), so in that case, you need to modify your constraints.Contiguity
O
0

swift 4. set the delegate

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
        scrollView.contentOffset.x = 0
    }
}
Organdy answered 11/3, 2019 at 7:23 Comment(0)
M
-6

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.

Melson answered 5/12, 2017 at 7:32 Comment(1)
This only disables the scroll bars (UI) but does not stop the actual scrolling interactionJanettjanetta

© 2022 - 2024 — McMap. All rights reserved.