简体   繁体   中英

How can I nest a UIScrollView inside a UIScrollView, so that the user can scroll the inner UIScrollView?

I haven't tried that yet, but I assume that I would have to disable scrolling of the parent scroll view as soon as I know that the user will want to scroll in the child scroll view, right?

Both scroll views are scrolling horizontal.

How could I disable scrolling detection of the parent temporarily? Or is there some other way?

UIScrollView has a property called scrollEnabled , which you can set to NO to disable scrolling in your parent scroll view.

I haven't tried it yet, but you may just find that it Does The Right Thing, ie, the inner scroll view scrolls until it can't scroll any more, at which point the outer scroll view scrolls.

It is very possible. The easiest way is to disable the scrollEnabled property, you can also try set the outer UIScrollView 's contentSize to its bounds, so that it won't scroll. Depending on what you need, you may set No to bounce as well but that'll basically make your scroll view like a regular UIView.

If you have control over all the child UIScrollView 's, you can implement UIScrollViewDelegate protocol, but if you don't, or your child UIScrollView is really a UITableView , or UICollectionView , then you can't really rely the UIScrollViewDelegate protocol, but remember UIScrollView comes with a panGestureRecognizer and you can play with that. A powerful but heavy handed way is to add an observer on the panGestureRecognizer 's state property so that you can capture all the events without being the delegate of the UIScrollView in question.

I did this and it does cause the parent scrollView NOT to scroll anymore which is fine but my child scrollView which was pushed via presentModalViewController still does not respond to scroll events (eg scrolling up or down). I'm obviously missing something...

EDIT : Got it to work finally by using 'scrollEnabled' on the parent view. Thanks.

I had this same problem till I set dynamically enough height to the contentSize of the inner ScrollView, using:

[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,600)];

Obviously this is just a hardcoded example, you should set the height needed to display the whole content of the view, and maybe no scroll is needed in the end.

It seems that setting the height of the scrollView using the IBuilder is not enough, so no scroll was needed and that's why I was thinking that the sweep event wasn't reaching the inner scroll.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM