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:30

That's strange, because whenever I create a scroll view with frame and content size within the bounds of the screen on either dimension, the scroll view does not scroll (or bounce) in that direction.

// Should scroll vertically but not horizontally
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.contentSize = CGSizeMake(320, 1000);

Are you sure the frame fits completely within the screen and contentSize's width is not greater than the scroll view's width?

查看更多
虎瘦雄心在
3楼-- · 2020-05-26 04:30

My version for webViews, a common solution:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

[webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];

}
查看更多
Deceive 欺骗
4楼-- · 2020-05-26 04:31

The checkbox for bounce vertically in storyboard-scrollview can simply help...

enter image description here

查看更多
走好不送
5楼-- · 2020-05-26 04:33

Something to keep in mind: You know there's nothing extra to see horizontally, but will your users know that? You may want a little horizontal bounce, even if there's no extra content to show horizontally. This let's the user know that their attempts to scroll horizontally are not being ignored, there's just nothing there for them to see. But, yeah, often you really don't want the bounce.

查看更多
我命由我不由天
6楼-- · 2020-05-26 04:34

That works for me in Swift:

scrollView.alwaysBounceHorizontal = false
scrollView.bounces = false
查看更多
登录 后发表回答