How to disable horizontal scrolling of UIScrollView?

How to disable horizontal scrolling of UIScrollView?

I have a UIView like iPhone's Springboard. I have created it using a UIScrollView and UIButtons. I want to disable horizontal scrolling on said scrollview. I want only vertical scrolling. How do i accomplish this?

Efficiency of create views programmatically vs IB


Round speedometer with Jquery / CSS in a browser
You have to set the contentSize property of the UIScrollView.

Am I using NSTimer correctly in iPhone View-based app?
For example, if your UIScrollView is 320 pixels wide (the width of the screen), then you could do this:.
Retrieve HTTPResponse/HTTPRequest status codes iPhone SDK?
CGSize scrollableSize = CGSizeMake(320, myScrollableHeight); [myScrollView setContentSize:scrollableSize]; 
The UIScrollView will then only scroll vertically, because it can already display everything horizontally..
UI iphone submit a form to javascript

How can I delay a method call for 1 second?

jQuery on iPhone/Android/BlackBerry


Will my iPhone app take a performance hit if I use Objective-C for low level code?
UPDATED: (After @EranMarom pointed out on his comment). You can stop horizontal scrolling or vertical scrolling in the ScrollViewDelegate Method.

Here it is how,. Stops Horizontal Scrolling:. If you want to scroll horizontally, then you need to increase the contentOffset.x.

Preventing that stops the scrollview scroll in horizontal direction..
- (void)scrollViewDidScroll:(UIScrollView *)sender {     sender.contentOffset.x = 0.0 } 
Stops Vertical Scrolling:. If you want to scroll vertically, then you need to increase the contentOffset.y.

Preventing that stops the scrollview scroll in vertical direction..
- (void)scrollViewDidScroll:(UIScrollView *)sender {     sender.contentOffset.y = 0.0 } 
Above code prevents the changes in x and y of a scrollview contentOffset and it leads to stop the scrolling in scrollViewDidScroll: method..


since iOS7 use .
self.automaticallyAdjustsScrollViewInsets = NO; 
//and create you 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]; 


Swift solution.
Create two outlets, one for your view and one for your scroll view:.
@IBOutlet weak var myView: UIView! @IBOutlet weak var scrollView: UIScrollView! 
Then in your viewDidLayoutSubviews you can add the following code:.
let scrollSize = CGSizeMake(myView.frame.width, myView.frame.height) scrollView.contentSize = scrollSize 
What we've done is collected the height and width of the view and set the scrollViews content size to match it.

This will stop your scrollview from scrolling horizontally.

. More Thoughts:. CGSizeMake takes a width & height using CGFloats.

You may need to use your UIScrollViews existing height for the second parameter.

Which would look like this:.
let scrollSize = CGSizeMake(myView.frame.width, scrollView.contentSize.height) 


Once I did it replacing the UIScrollView with a UITableView with only 1 cell, it worked fine..


I struggled with this for some time trying unsuccessfully the various suggestions in this and other threads.

However, in another thread (not sure where) someone suggested that using a negative constraint on the UIScrollView worked for him.

So I tried various combinations of constraints with inconsistent results.

What eventually worked for me was to add leading and trailing constraints of -32 to the scrollview and add an (invisible) textview with a width of 320 (and centered)..

85 out of 100 based on 55 user ratings 955 reviews