UIScrollView Not Scrolling?

2020-02-10 04:50发布

I have a UIScrollView created in interface builder with a bunch of UITextView and UIImageView objects already added. I have it connected to an IBOutlet, and under -(void)viewDidLoad I set its content size to 1200x2000. User interaction Enabled, Multitouch, Scrolling Enabled and shows vertical and horizontal scroll indicators are all set to YES. When I launch the app in the iOS simulator, it doesn't scroll. What could possibly be the cause of this behavior?

6条回答
Explosion°爆炸
2楼-- · 2020-02-10 05:00

I use a custom view on the UIScrollView, for pull to refresh function, and the scroll view is not scrollable. I try set contentSize with AutoLayout, and try set it by codes too. The scroll view still cannot scroll.

After spending 3 ~ 4 hours, I solve it by contentInset. It will tell scroll view to add some extra space for scrolling.

override func viewDidLayoutSubviews() {
    // Add extra 15pt on the `top` of scrollView.
    //self.scrollView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)

    // Add extra 2pt on the `bottom` of scrollView, let it be scrollable. And the UI is more beautiful in my case. 
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 2, 0)
}

Reference docs:

查看更多
Animai°情兽
3楼-- · 2020-02-10 05:00

viewDidLoad is indeed too soon, but viewDidAppear:animated might not be late enough either. The ideal place to set the contentOffset is inside viewDidLayoutSubviews.

查看更多
何必那么认真
4楼-- · 2020-02-10 05:09
  1. viewDidLoad is to soon. You must wait until after the layout of the views has taken place. Try setting the contentSize in viewDidAppear.

  2. Are you using autolayout? If so, check out the following answer (and forget 1.): UIScrollView doesn't use autolayout constraints.

查看更多
成全新的幸福
5楼-- · 2020-02-10 05:10

This is a good walk-through of scroll-views in case anyone forgot after not using them for a while: https://medium.com/@pradeep_chauhan/how-to-configure-a-uiscrollview-with-auto-layout-in-interface-builder-218dcb4022d7.

Basically, make sure you have view -> scrollview -> view, like so:
enter image description here
then,
1. Set scroll view constraint (top, bottom, leading and trailing) to (0,0,0,0).
2. Set inner view constraint (top, bottom, leading and trailing) to (0,0,0,0).
3. Set inner view to have equal width and equal height with parent view.
4. Select height constraint of inner view and set priority to low (250).

查看更多
干净又极端
6楼-- · 2020-02-10 05:11

The best way I know how to overcome this problem without resorting to writing code is that it all can be done within a storyboard and here is how:

  1. Leave autolayout enabled.
  2. Make a generic UIView a subview of the UIScrollView. 2.1)Put all your View components inside this generic UIView.
  3. Make a autolayout constraint between a subview and the UIScrollView. You must pin the right and/or bottom of the last subview to the right and/or bottom of the UIScrollView. This is how the UIScrollView knows the content size.
查看更多
别忘想泡老子
7楼-- · 2020-02-10 05:15

If you're using Autolayout, make sure you put the code to set Content size within

viewDidLayoutSubviews
查看更多
登录 后发表回答