How to disable horizontal scrolling of UIScrollView?

I have a UIView as an iPhone Springboard. I created it using UIScrollView and UIButtons . I want to disable horizontal scrolling for the specified scroll view. I want only vertical scroll. How should I do it?

+83
ios objective-c iphone xcode uiscrollview
Jun 30 '09 at 9:29
source share
12 answers

You need to set the contentSize property for UIScrollView . For example, if your UIScrollView has a width of 320 pixels (screen width), you can do this:

 CGSize scrollableSize = CGSizeMake(320, myScrollableHeight); [myScrollView setContentSize:scrollableSize]; 

UIScrollView will scroll only vertically, because it can already display everything horizontally.

+119
Jun 30 '09 at 16:31
source share

UPDATED: (after @EranMarom pointed out his comment)

You can stop horizontal scrolling or vertical scrolling in the ScrollViewDelegate method. Like this,

Stop horizontal scrolling:

If you want to scroll horizontally, you need to increase contentOffset.x. Prevent horizontal scroll scrolling from stopping.

 - (void)scrollViewDidScroll:(UIScrollView *)sender { sender.contentOffset.x = 0.0 } 

Stop vertical scroll:

If you want to scroll vertically, you need to increase contentOffset.y. Prevents stopping scroll scrolling in the vertical direction.

 - (void)scrollViewDidScroll:(UIScrollView *)sender { sender.contentOffset.y = 0.0 } 

The above code prevents changes in x and y for scrollview contentOffset and causes the scroll to stop in the scrollViewDidScroll: method.

+84
Jun 09 '13 at 12:08 on
source share

since iOS7 uses

 self.automaticallyAdjustsScrollViewInsets = NO; 

// and create a 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]; 
+10
Jan 22 '14 at 10:45
source share

Fast decision

Create two exits: one for your view and one for the scroll view:

 @IBOutlet weak var myView: UIView! @IBOutlet weak var scrollView: UIScrollView! 

Then in your viewDidLayoutSubviews you can add the following code:

 let scrollSize = CGSize(width: myView.frame.size.width, height: myView.frame.size.height) scrollView.contentSize = scrollSize 

We assembled the height and width of the view and set the size of the contents of scrollViews. This will scroll horizontally.




Other thoughts:

CGSizeMake uses width and height with CGFloats. You may need to use the existing UIScrollViews height for the second parameter. What would look like this:

 let scrollSize = CGSize(width: myView.frame.size.width, height: scrollView.contentSize.height) 
+10
Sep 02 '15 at 21:36
source share

You can select a view and then the Attributes Inspector uncheck User Interaction Enabled .

+4
Nov 07 '16 at 2:14
source share

As soon as I replaced this with a UIScrollView using a UITableView with only 1 cell, it worked fine.

0
May 20 '15 at 11:31
source share

I struggled with this for some time, unsuccessfully trying to use various offers 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 ultimately worked for me was to add restrictions on moving and dragging -32 to the scrollview and add a (invisible) text view with a width of 320 (and centered).

0
Jun 18 '15 at 15:26
source share

I had a tableview contentInset set to viewDidLoad (as shown below), which causes horizontal scrolling

 self.tableView.contentInset = UIEdgeInsetsMake(0, 30, 0, 0); 

Check if there is any tableview file installed for various reasons and disable it.

0
May 22 '17 at 8:27
source share

Try the following:

 CGSize scrollSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, scrollHeight); [scrollView setContentSize: scrollSize]; 
0
Aug 19 '17 at 17:24
source share

Use this single line.

self.automaticallyAdjustsScrollViewInsets = NO;

0
Oct 06 '17 at 18:17
source share

In my case, the width of the contentView was greater than the width of the UIScrollView, and this was the reason for the unwanted horizontal scroll. I solved this by setting the width of the contentView to the width of the UIScrollView.

Hope this helps someone

0
Oct 31 '18 at 11:18
source share

In my case with Swift 4.2 you can use:

Disable vertical scrolling:

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

Disable horizontal scrolling:

 func scrollViewDidScroll(_ scrollView: UIScrollView) { scrollView.contentOffset.x = 0.0 } 
0
Jun 18 '19 at 14:24
source share



All Articles