How do I stop a UIScrollView from bouncing horizon

2020-05-26 03:29发布

I have a UIScrollView that shows vertical data, but where the horizontal component is no wider than the screen of the iPhone. The problem is that the user is still able to drag horizontally, and basically expose blank sections of the UI. I have tried setting:

scrollView.alwaysBounceHorizontal = NO;
scrollView.directionalLockEnabled = YES;

Which helps a little, but still doesn't stop the user from being able to drag horizontally. Surely there is a way to fix this easily?

11条回答
放我归山
2楼-- · 2020-05-26 04:10

In my case, i just need to set this line:

collectionView.bounces = false

查看更多
Deceive 欺骗
3楼-- · 2020-05-26 04:13

Try setting scrollView.bounces to NO and scrollView.alwaysBounceVertical to YES.

查看更多
Animai°情兽
4楼-- · 2020-05-26 04:15

Whether or not a view scrolls (and bounces) horizontally depends on three things:

  • The content size
  • The left and right content insets
  • The width of the scroll view -

If the scroll view can fit the content size plus the insets then it doesn't scroll or bounce.

Avoid horizontal bouncing like so:

scrollView.contentSize = CGSizeMake(scrollView.frame.size.width - scrollView.contentInset.left - scrollView.contentInset.right, height);

I am adding this answer because the previous ones did not take contentInset into account.

查看更多
虎瘦雄心在
5楼-- · 2020-05-26 04:20
scrollView.bounces = NO;

Worked for me.

查看更多
何必那么认真
6楼-- · 2020-05-26 04:23

If anyone developing for OS X is looking here, as of OS X 10.7, the solution is to set the horizontalScrollElasticity property to false/NO on the scroll view, like this:

Swift:

scrollView.horizontalScrollElasticity = false

Objective-C:

scrollView.horizontalScrollElasticity = NO
查看更多
Juvenile、少年°
7楼-- · 2020-05-26 04:25

Make sure the UIScrollView's contentSize is not wider than the UIScrollView itself. In my own apps this was enough to avoid horizontal scrolling, except in cases where I got really crazy swiping in random directions (e.g., starting a scroll while the view was still decelerating).

查看更多
登录 后发表回答