Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock the horizontal scrolling of a scrollView in iOS

i have a scrollView. i want it to scroll only in one direction(VERTICAL). Is there a way in which i can lock the horizontal scrolling...?? ...

like image 848
A for Alpha Avatar asked Mar 11 '11 10:03

A for Alpha


People also ask

How do I lock horizontal scrolling?

Since horizontal scrolling is generally a bad idea, this rule can come in handy. To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.

How do I scroll horizontally in Safari?

Hold Shift & scroll mouse wheel.


2 Answers

Same (adapted) answer for you as in:

Disabling vertical scrolling in UIScrollView

right, all answers here are ok...

but if for some strange reason your contentSize should be higher than the UIScrollView in both dimensions, you can disable the horizontal scrolling implementing the UIScrollView protocol method -(void)scrollViewDidScroll:(UIScrollView *)aScrollView

just add this in your UIViewController

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

it's just the method called when the user scroll your UIScrollView, and doing so you force the content of it to have always the same .x

like image 95
meronix Avatar answered Sep 20 '22 04:09

meronix


self.scrollview.contentSize = CGSizeMake(1, self.scrollview.frame.size.height * number_of_items); 

should help. I just locked my scroll view horizontally, by doing the similar technique.

like image 20
Sean S Lee Avatar answered Sep 18 '22 04:09

Sean S Lee