How to make uiscrollview only vertical scrolling for ios?

I am sure there must be other ways to do this but a quick fix is :

1.) Create a width constraint on ContentView in Storyborad.

2.) IBOutlet that widthContraint and set its value to the view frame width in viewDidLoad.

Suppose the name of the constraint outlet is contentViewWidthContraint.

contentViewWidthContraint.constant = self.view.bounds.size.width;

Another alternative to do so from Storyboard, is to fix the Contentview width to the view's width from the storyboard or to the Scrollview, if Scrollview already has a Equal width contraint with superview . Add the "Equal Width" contraint from Contentview to either self.view or to Scrollview (if scrollview, already has the width contraint)


In the Storyboard set the width of the elements contained in your UIScrollView equal to the width of this UIScrollView (by selecting all elements and the UIScrollView holding in the panel on the left of your Storyboard and then setting the 'Equal Widths' constraint under 'Pin' on the bottom of your Storyboard). Just pinning the right sides of the elements to that of the UIScrollView won't work as it will adjust the size of its "display view" to the width of the largest element and if this is smaller than the width of the UIScrollView all elements will just appear aligned to its left side.


There is also another possibility that offers a very good result.

You can mark a checkbox:

enter image description here

O programmatically:

scrollView.alwaysBounceVertical = true

Have you set up the "ContentView" width to match with the scroll view width? I had the same problem and I fixed with "Equal Widths".

"Equal Widths" will tell to your "ContentView" to use the same width of the "Scroll View", which should be fitting the screen if you have set up the constrain properly.

You can do this easily on the storyboard.

  1. Drag and drop, with right click (important!!!), from "ContentView" to "ScrollView"

  2. Release the click, you will be prompted with a menu, select "Equal Widths".

    Prompted menu

This should fix your problem using the scrollview with AutoLayout from Storyboard editor.

You can find a full tutorial how to use ScrollView with Autolayout and Storyboard here.

I hope this is useful for you :)