26

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

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
A for Alpha
  • 2,904
  • 8
  • 42
  • 76

11 Answers11

26

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

Community
  • 1
  • 1
meronix
  • 6,175
  • 1
  • 23
  • 36
  • 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
13
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.

Sean S Lee
  • 1,274
  • 9
  • 24
10

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];    
}
Mike Hay
  • 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
2

See this question, you have to set the scrollable size to the corresponding size of the view so that there is nothing to scroll.

Community
  • 1
  • 1
Tomas Vana
  • 18,317
  • 9
  • 53
  • 64
2

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?

occulus
  • 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
2

You can try this,

self.ScrollView.contentSize = CGSizeMake(self.ScrollView.frame.size.width, 1450);

here 1450 is my view height.

Sharme
  • 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
1

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];
Mirko Brunner
  • 2,204
  • 2
  • 21
  • 22
1

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)]

user655152
  • 11
  • 1
0

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];
animuson
  • 53,861
  • 28
  • 137
  • 147
BCI
  • 465
  • 6
  • 19
0

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
Community
  • 1
  • 1
Jack
  • 13,571
  • 6
  • 76
  • 98
0

another solution : You can set the align top position to -200 or whatever value you want.

Raouf
  • 1
  • 1