Disabling vertical scrolling in UIScrollView

IphoneIosUiscrollview

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

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

Solution 3 - Iphone

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

Solution 4 - Iphone

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.

Solution 5 - Iphone

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

Solution 6 - Iphone

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.

Solution 7 - Iphone

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    [aScrollView setContentOffset: CGPointMake(aScrollView.contentOffset.x,0)];

}

you must have confirmed to UIScrollViewDelegate

aScrollView.delegate = self;

Solution 8 - Iphone

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.

Solution 9 - Iphone

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];
}

Solution 10 - Iphone

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
}

Solution 11 - Iphone

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.

Solution 12 - Iphone

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

Solution 13 - Iphone

I think you can

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

That should work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsome_idView Question on Stackoverflow
Solution 1 - IphoneMirko BrunnerView Answer on Stackoverflow
Solution 2 - IphoneySgPjxView Answer on Stackoverflow
Solution 3 - IphonemeronixView Answer on Stackoverflow
Solution 4 - IphoneFebin PView Answer on Stackoverflow
Solution 5 - IphoneManiView Answer on Stackoverflow
Solution 6 - IphonebolonnView Answer on Stackoverflow
Solution 7 - Iphoneuser3693546View Answer on Stackoverflow
Solution 8 - IphonemanuelsantosView Answer on Stackoverflow
Solution 9 - IphoneMichaelView Answer on Stackoverflow
Solution 10 - Iphoneregina_fallangiView Answer on Stackoverflow
Solution 11 - IphonemalexView Answer on Stackoverflow
Solution 12 - IphoneiWheelBuyView Answer on Stackoverflow
Solution 13 - IphoneJerome LiView Answer on Stackoverflow