How do I stop a UIScrollView from bouncing horizontally?

后端 未结 11 1772
名媛妹妹
名媛妹妹 2021-02-05 12:07

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

相关标签:
11条回答
  • 2021-02-05 12:10

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

    0 讨论(0)
  • 2021-02-05 12:10

    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
    
    0 讨论(0)
  • 2021-02-05 12:12

    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.

    0 讨论(0)
  • 2021-02-05 12:15

    My version for webViews, a common solution:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];
    
    }
    
    0 讨论(0)
  • 2021-02-05 12:20

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

    collectionView.bounces = false

    0 讨论(0)
  • 2021-02-05 12:22

    That works for me in Swift:

    scrollView.alwaysBounceHorizontal = false
    scrollView.bounces = false
    
    0 讨论(0)
提交回复
热议问题