89

There is an option in IB to uncheck vertical scrolling on a scrollview, but it doesnt seem to work.

How can the scrollview be set to only scroll horizontally, and not vertically in the code rather than IB?

1

13 Answers 13

138

since iOS7:

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

second: in your initWithNibName:

self.automaticallyAdjustsScrollViewInsets = NO;

That´s it.

8
  • Wonderful solution. Worked like a charm. Will it affect the size of the rest of the views in my ViewController? (Buttons, Labels..) I mean, "self" is an UIViewController in my case
    – voghDev
    Dec 17, 2014 at 9:23
  • @voghDev no it has no effects to the subviews in your scrollview. Dec 17, 2014 at 13:18
  • Amazing added this to the viewdidload method on my view controllers that were having issues. Jul 10, 2015 at 11:25
  • if you're using navigationController and your scrollview is behind the navigationBar's title like mine use under viewDidLoad method. It solves.
    – EFE
    Jul 15, 2015 at 10:53
  • 1
    You can achieve the same in the storyboard settings directly without any line of code. Just select the scroll view controller and uncheck the "Adjust Scroll View Insets" in the attribute inspector.
    – Morpheus78
    May 22, 2016 at 17:54
124

Try setting the contentSize's height to the scrollView's height. Then the vertical scroll should be disabled because there would be nothing to scroll vertically.

scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
3
  • You just saved me a lot of time debugging this weird issue. Thanks!
    – jpm
    Sep 25, 2013 at 2:41
  • 2
    @fabb how does one know when the frame is changed?
    – Sam
    Aug 13, 2014 at 14:39
  • I think in auto-layout era this is outdated now. Apr 21, 2022 at 10:06
49

yes, pt2ph8's answer is right,

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

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

just add this in 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 the method called when the user scroll your UIScrollView, and doing so you force the content of it to have always the same .y

1
  • 7
    God! Please DO NOT declare the oldY variable in .m file like float oldY; refer to the above code. That is a C-style variable symbol for per application, the variable MUST be declared as an instance member of controller class.
    – Itachi
    Nov 17, 2015 at 4:07
21

Include the following method

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

and set the content size width of the scroll view equal to the width of the scroll view.

2
  • 1
    This can also be done in Storyboard by selecting the View Controller and unchecking the property there
    – justColbs
    Aug 16, 2016 at 21:25
  • 1
    I don't like this because it sets it for all scroll views... Wish you could be specific to one scroll view on the diagram w/o creating a new ScrollView class etc
    – visc
    Mar 18, 2019 at 16:02
20

You need to pass 0 in content size to disable in which direction you want.

To disable vertical scrolling

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

To disable horizontal scrolling

scrollView.contentSize = CGSizeMake(0,scrollView.contentSize.height);
4
  • 1
    This will set the contentSize of the view to have 0 on the axis you set. ContentSize is referring to the size of the content, so we want to pass in the height of the screen to disable the vertical scrolling beyond that frame. the solution marked as correct, is correct.
    – some_id
    Feb 13, 2016 at 7:55
  • This worked well for me. setting height as contentSize's height still got me vertical scrolling. Jul 15, 2016 at 4:45
  • 1
    This did not work for me. Also tried self.automaticallyAdjustsScrollViewInsets Mar 21, 2018 at 10:41
  • This is it! Setting to the height of the Scroll View didn't work for me, but this did
    – Echizzle
    Jun 10, 2020 at 1:12
18

I updated the content size to disable vertical scrolling, and the ability to scroll still remained. Then I figured out that I needed to disable vertical bounce too, to disable completly the scroll.

Maybe there are people with this problem too.

3
  • You saved me. My contentSize and frame were the same and I could not for the life of me figure out what was wrong. Thanks a bunch!!
    – yuf
    Dec 13, 2012 at 6:03
  • 2
    Your answer is why I love stackoverflow! :-) Holy smoke... this drove me nuts! You are right. It's the bounce effect. I want the bounce effect, but not when there is no need to scroll, so I'll be looking to disable/re-enable that effect now in code depending on whether there is a need to scroll. Mar 16, 2015 at 11:30
  • 2
    Okay, very easy to enable/disable the bounces on a UIScrollView at runtime. In my case I only care about the height of the scroll content and this did the trick: self.scrollView.bounces = (BOOL)(self.scrollView.contentSize.height > self.scrollView.frame.size.height); Mar 16, 2015 at 11:38
12
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x,0)];

}

you must have confirmed to UIScrollViewDelegate

aScrollView.delegate = self;
0
5

Just set the y to be always on top. Need to conform with UIScrollViewDelegate

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

This will keep the Deceleration / Acceleration effect of the scrolling.

4

On iOS 11 please remember to add the following, if you're interested in creating a scrollview that sticks to the screen bounds rather than a safe area.:

if (@available(iOS 11.0, *)) {
    [self.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
1
  • 1
    after all. thank you thousand times sir! you saved my day
    – ha100
    Jul 16, 2018 at 9:20
2

A lot of answers include setting the contentOffset to 0. I had a case in which I wanted the view inside the scrollView to be centered. This did the job for me:

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollView.contentOffset.y = -scrollView.contentInset.top
}
1
  • Works well in auto-layout. Apr 21, 2022 at 10:06
1

From iOS11 one can use the following property

let frameLayoutGuide: UILayoutGuide

If you set constraints for frameLayoutGuide.topAnchor and frameLayoutGuide.bottomAnchor to the same anchors of some subview of your scrollView then vertical scroll will be disabled and the height of the scrollView will be equal to the height of its subview.

1

The most obvious solution is to forbid y-coordinate changes in your subclass.

override var contentOffset: CGPoint {
  get {
    return super.contentOffset
  }
  set {
    super.contentOffset = CGPoint(x: newValue.x, y: 0)
  }
}

This is the only suitable solution in situations when:

  1. You are not allowed or just don't want to use delegate.
  2. Your content height is larger than container height
2
0

I think you can

  1. set directionalLockEnabled to true
  2. set the contentView's width equals to superView's width.

That should work.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.