Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable directional lock for a UIScrollView?

I have a UIScrollView with a UIView inside. I want to lock the x-axis so that the view is only scrolled vertically. How do I enable directional locking?

like image 760
RexOnRoids Avatar asked May 14 '09 07:05

RexOnRoids


People also ask

What is scroll view in Swift?

The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.


1 Answers

First, set the UIScrollView's contentSize to have a width that is equal to or less than the width of the UIScrollView's frame.

Next, set UIScrollView's alwaysBounceHorizontal to NO. This will prevent the scroll view from "rubber banding" even though you've told it there's no more horizontal content to display.

UIScrollView *scrollView;
CGSize size = scrollView.contentSize;
size.width = CGRectGetWidth(scrollView.frame);
scrollView.contentSize = size;
scrollView.alwaysBounceHorizontal = NO;

It doesn't matter what's actually inside the scroll view.

Swift 5.0

let scrollView = UIScrollView() // Or however you want to initialize it
var size = scrollView.contentSize
size.width = scrollView.frame.width
scrollView.contentSize = size
scrollView.alwaysBounceHorizontal = false
like image 137
Nick Farina Avatar answered Oct 22 '22 12:10

Nick Farina