How to disable horizontal scrolling of UIScrollVie

2019-01-10 07:23发布

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?

11条回答
欢心
2楼-- · 2019-01-10 07:55

Use this single line.

self.automaticallyAdjustsScrollViewInsets = NO;

查看更多
我命由我不由天
3楼-- · 2019-01-10 07:57

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

查看更多
闹够了就滚
4楼-- · 2019-01-10 08:01

You have to set the contentSize property of the UIScrollView. For example, if your UIScrollView is 320 pixels wide (the width of the screen), then you could do this:

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

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

查看更多
淡お忘
5楼-- · 2019-01-10 08:01

Try This:

CGSize scrollSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, scrollHeight);
[scrollView setContentSize: scrollSize];
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-10 08:02

In my case the width of the contentView was greater than the width of UIScrollView and that was the reason for unwanted horizontal scrolling. I solved it by setting the width of contentView equal to width of UIScrollView.

Hope it helps someone

查看更多
登录 后发表回答