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...?? ...

- 71,928
- 54
- 216
- 264

- 2,904
- 8
- 42
- 76
-
Please tell us more about why you want to do that, it helps us answer the question. – occulus Mar 11 '11 at 12:04
11 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
-
Hey I want to lock it in the Y direction (so the user is only able to scroll horizontally), when I change the `aScrollView.contentOffset.y` to `aScrollView.contentOffset.x` it just does not scroll in any direction anymore... (the one you've given does work as it should) Can you help me please? – LinusGeffarth Dec 13 '14 at 08:43
-
1@ummesekatze: try: [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x, oldY)]; – meronix Dec 14 '14 at 09:48
-
@meronix Thank you so much for this solution! I've struggled with this issue for days until finally coming across your solution! I added a bit more to your solution: `- (void)scrollViewDidScroll:(UIScrollView *)sender { [scrollView setContentOffset: CGPointMake(oldX, scrollView.contentOffset.y)]; if (sender.contentOffset.x != 0) { CGPoint offset = sender.contentOffset; offset.x = 0; sender.contentOffset = offset; }` – Joshua Hart Nov 10 '15 at 10:36
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.

- 1,274
- 9
- 24
The safest and most successful method I've found to constrain the movement of a scroll view is to subclass UIScrollView and override setContentOffset:animated: and setContentOffset: methods (code below).
The advantage of overriding these methods is that it directly alters the requested contentOffset before any of the UIKit code starts to act on it, avoiding any of the side effects that can occur when modifying the contentOffset in scrollViewDidScroll: or other UIScrollViewDelegate methods.
It takes just a few minutes to create the new subclass, and it works like a charm.
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
// restrict movement to vertical only
CGPoint newOffset = CGPointMake(0, contentOffset.y);
[super setContentOffset:newOffset animated:animated];
}
- (void)setContentOffset:(CGPoint)contentOffset {
// restrict movement to vertical only
CGPoint newOffset = CGPointMake(0, contentOffset.y);
[super setContentOffset:newOffset];
}

- 2,828
- 21
- 26
-
I like this solution because you can also use it in Interface Builder by setting the Custom Class from UIScrollView to the subclass. +1 for that. – Brainware Aug 06 '13 at 19:25
See this question, you have to set the scrollable size to the corresponding size of the view so that there is nothing to scroll.

- 1
- 1

- 18,317
- 9
- 53
- 64
If the UIScrollView's contentSize
property has the same as the actual width of the UIScrollView, then no horizontal scrolling will be possible.
Are you wanting to stop horizontal scrolling when horizontal scrolling would be valid? (i.e. the contentSize
is wider than the actual UIScrollView width?) If 'yes', why are you wanting to do that?

- 16,959
- 6
- 53
- 76
-
@ occulus:yes occulus..... i want to stop it even when the content size is greater than the actual scrollView width – A for Alpha Mar 11 '11 at 10:45
-
Ok, have updated my answer and asked "Why are you wanting to do that?". Let me know because it affects the strategy. – occulus Mar 11 '11 at 11:46
You can try this,
self.ScrollView.contentSize = CGSizeMake(self.ScrollView.frame.size.width, 1450);
here 1450 is my view height.

- 625
- 4
- 10
- 28
-
You mark the text that is supposed to be code, then you click the code hightlighter button. Or you indent with four spaces. That makes the text get code formatted. – Phil Dec 06 '11 at 13:15
oh since iOS7 use this (in initWithNibName):
self.automaticallyAdjustsScrollViewInsets = NO;
and create the page scroller with 3 pages
self.pageView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.pageView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];
[self.pageView setShowsVerticalScrollIndicator:NO];
[self.pageView setPagingEnabled:YES];
[self.view addSubview:self.pageView];

- 2,204
- 2
- 21
- 22
The scrollView is only scrollable horizontally if you set the contentSize width bigger than the scrollView frame. If you set the contentSize width to the same as the scrollview it won't scroll. For example
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,100,100)]
[scrollView setContentSize:CGSizeMake(100, 300)]

- 11
- 1
In my case the contentSize was higher than the UIScrollView for x and y. For me meronixe's answer worked out quite well, but was for my needs not quite complete. Unfortunately the horizontal scrollbar was still visible.
This I could change with the following code in
- (void)webViewDidFinishLoad:(UIWebView *)webView
[self.webView.scrollView setShowsHorizontalScrollIndicator:NO];
Swift
Make contentOffset.x = 0
for disable horizontal scroll
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.x = 0//to lock horizontal scrolling
//scrollView.contentOffset.y = 0//to lock vertical scrolling
}
}
Additionally you can disable scroll indicator as
scrollView.showsHorizontalScrollIndicator = false//to disable HorizontalScrollIndicator
scrollView.showsVerticalScrollIndicator = false//to disable VerticalScrollIndicator
another solution : You can set the align top position to -200 or whatever value you want.

- 1
- 1