UIScrollView not scrolling

2019-01-03 08:53发布

I have a UIScrollView which contains many UIImageViews, UILabels, etc... the labels are much longer that the UIScrollView, but when I run the app, I cannot click and scroll down...

Why might this be?

Thanks

23条回答
别忘想泡老子
2楼-- · 2019-01-03 09:13

The answer above is correct - to make scrolling happen, it's necessary to set the content size.

If you're using interface builder a neat way to do this is with user defined runtime attributes. Eg:

enter image description here

查看更多
Bombasti
3楼-- · 2019-01-03 09:14

Alot of the time the code is correct if you have followed a tutorial but what many beginners do not know is that the scrollView is NOT going to scroll normally through the simulator. It is suppose to scroll only when you press down on the mousepad and simultaneously scroll. Many Experienced XCode/Swift/Obj-C users are so use to doing this and so they do not know how it could possibly be overlooked by beginners. Ciao :-)

@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(scrollView)
    // Do any additional setup after the view
}

override func viewWillLayoutSubviews(){
    super.viewWillLayoutSubviews()
    scrollView.contentSize = CGSize(width: 375, height: 800)
}

This code will work perfectly fine as long as you do what I said up above

查看更多
forever°为你锁心
4楼-- · 2019-01-03 09:14

I had the same issue in IB.

After setting the leading, trailing, top and bottom of the scrollView to its superView. I made the following changes to make the containerView scrollable, which worked.

To make the scrollView only scroll on horizontal direction make the constraint with scrollView's centerY = ContainerView's centerY

and to make it vertically scrollable make the scrollView's centerX = ContainerView's centerX

查看更多
来,给爷笑一个
5楼-- · 2019-01-03 09:17

In my case I had to set delaysContentTouches to true because the objects inside the scrollView were all capturing the touch events and handling themselves rather than letting the scrollView itself handle it.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-03 09:17

If your scrollView is a subview of a containerView of some type, then make sure that your scrollView is within the frame or bounds of the containerView. I had containerView.clipsToBounds = NO which still allowed me see the scrollView, but because scrollView wasn't within the bounds of containerView it wouldn't detect touch events.

For example:

containerView.frame = CGRectMake(0, 0, 200, 200);
scrollView.frame = CGRectMake(0, 200, 200, 200);
[containerView addSubview:scrollView];
scrollView.userInteractionEnabled = YES;

You will be able to see the scrollView but it won't receive user interactions.

查看更多
我命由我不由天
7楼-- · 2019-01-03 09:19

Set contentSize property of UIScrollview in ViewDidLayoutSubviews method. Something like this

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    scrollView.contentSize = CGSizeMake(view.frame.size.width, view.frame.size.height)
}
查看更多
登录 后发表回答