Turn off vertical scrolling in a UIScrollView

IB has an option to undo vertical scrolling on scrollview, but it does not seem to work.

How can I scroll only horizontally, and not vertically in the code, and not in IB?

+69
ios iphone uiscrollview
Feb 23 '11 at 18:59
source share
8 answers

Try adjusting the height of the contentSize to the height of the scrollView. Then vertical scrolling should be disabled because there will be nothing to scroll vertically.

scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height); 
+105
Feb 23 '11 at 19:03
source share

since iOS7:

first: the width of the content size should be equal to the width of your scrollview

second: in initWithNibName file:

 self.automaticallyAdjustsScrollViewInsets = NO; 

That's all.

+124
Jan 22 '14 at 10:42 on
source share

yes, pt2ph8 answer is correct,

but if for some strange reason your contentSize should be higher than UIScrollView, you can disable vertical scrolling that implements the UIScrollView protocol method

  -(void)scrollViewDidScroll:(UIScrollView *)aScrollView; 

just add this to your UIViewController

 float oldY; // here or better in .h interface - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, oldY)]; // or if you are sure you wanna it always on top: // [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, 0)]; } 

it's just a method called when the user scrolls your UIScrollView and makes sure its contents are always the same .y

+47
Feb 23 2018-11-28T00:
source share

Include the following method

 -(void)viewDidLayoutSubviews{ self.automaticallyAdjustsScrollViewInsets = NO; } 

and set the width of the scroll content to the width of the scroll.

+22
Nov 17 '14 at 11:03
source share

I updated the content size to disable vertical scrolling, and the ability to scroll is still there. Then I realized that I needed to disable the vertical bounce in order to completely disable the scroll.

Perhaps there are people with this problem.

+16
Jul 19 2018-12-12T00:
source share
 - (void)scrollViewDidScroll:(UIScrollView *)aScrollView { [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x,0)]; } 

you must confirm UIScrollViewDelegate

 aScrollView.delegate = self; 
+12
Mar 18 '15 at 17:34
source share

You need to pass 0 in the size of the content to disable it in the right direction.

To disable vertical scrolling

 scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,0); 

To disable horizontal scrolling

 scrollView.contentSize = CGSizeMake(0,scrollView.contentSize.height); 
+10
Feb 13 '16 at 5:55
source share

Just set y always on top. Must match UIScrollViewDelegate

 func scrollViewDidScroll(scrollView: UIScrollView) { scrollView.contentOffset.y = 0.0 } 

This will keep the scroll deceleration / acceleration effect.

+2
Jan 21 '16 at 6:27
source share



All Articles