7

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?

iNiravKotecha
  • 2,409
  • 1
  • 10
  • 24
Hiren Prajapati
  • 667
  • 2
  • 9
  • 22

6 Answers6

7

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

enter image description here

ManjunathK
  • 143
  • 1
  • 11
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
        }
    }
    
}

mohsen
  • 2,711
  • 1
  • 19
  • 41
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

Sam
  • 277
  • 1
  • 27
  • 91
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)];
mschmidt
  • 2,610
  • 4
  • 15
  • 27
  • 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. – rob mayoff Dec 04 '17 at 18:58
0

swift 4. set the delegate

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x != 0 {
        scrollView.contentOffset.x = 0
    }
}
Ryan110
  • 638
  • 4
  • 18
-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.