Alexey Zakevich
Alexey Zakevich

Reputation: 131

How to make uiscrollview only vertical scrolling for ios?

I'm trying to make layout inside scrollview using this one tutorial link

And get the following result link

It will be appreciated for any advices or tutorial links. It needs only vertical scrolling

Upvotes: 12

Views: 29325

Answers (9)

Jesuslg123
Jesuslg123

Reputation: 735

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

Upvotes: 11

Vitalii
Vitalii

Reputation: 4437

This is because scroll view have no idea where your content should end.

But when at least one item inside your scroll view has its "trailing space" constraint attached to a view outside the scroll view (usually a view the scroll view is sitting in or some other view of a higher level, which "knows" its width) - the scroll view will automatically get an idea about your wanted width and won't scroll horizontally (unless that trailing constraint implies having your content outside the screen).

Better if all items inside scroll view have their "trailing space" constraints connected either to each other or to a view outside the scroll view. But not the scroll view itself.

No additional code or extra constraints needed for this to work.

Upvotes: 1

Markus
Markus

Reputation: 1177

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

Upvotes: 3

Jen Jose
Jen Jose

Reputation: 4035

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)

Upvotes: 11

MCS
MCS

Reputation: 31

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.

Upvotes: 3

Vineeth Joseph
Vineeth Joseph

Reputation: 5187

For disabling the horizontal scroll, you can set the content size in the -(void)scrollViewDidScroll method.

[self.scrollView setContentOffset: CGPointMake(0, self.scrollView.contentOffset.y)];
self.scrollView.directionalLockEnabled = YES;

Upvotes: 1

iYoung
iYoung

Reputation: 3622

Try to set it's width to 0 & height equal to content size like this:

self.scrollView.contentSize = CGSizeMake(0, self.scrollView.contentSize.height);

This will work as you want. Try it & tell if still facing any issue.

Upvotes: 1

M Abdul Sami
M Abdul Sami

Reputation: 1531

You need to set the width of UIScrollView equal to or less than the width of your Parent View. Two ways to do it:

1) You can do this in Storyboard via layout constraints

2) You can do this programatically:

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.contentSize.height);

Upvotes: 0

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

Too set UIScrollView constraints as like below code so it will occupied whole screen.Not exceed the screen size.

Leading Space = 0 from mainView
Top Space = 0 from mainView
Bottom Space = 0 from mainView 
Trailing Space = 0 from mainView

Upvotes: 0

Related Questions