5

I've looked through various SO questions on the topic and I have not found a solution. I have a UIViewController with a UITableView and a UICollectionView. I want the UICollectionView to scroll to the top, when the user taps it.

The documents say if you have more than one UiScrollView subclass - you need to set them to no and the UiScrollView you want to scroll to the top, to yes.

So I wrote this bit of code to go through all my views:

for (UIScrollView *view in self.view.subviews) {
                    if ([view isKindOfClass:[UIScrollView class]]) {
                        view.scrollsToTop = NO;
                    }
                }

                self.collectionView.scrollsToTop = YES;

This way I am sure any subclass of UiScrollView has it's scrollsToTop property set to no.

However tapping on the status bar does not do anything.

Can someone tell me what I am missing here?

Thank you

3
  • Have you confirmed that none of the subviews contain a subclassed UIScrollView?
    – sooper
    Jun 23, 2014 at 11:38
  • The code I posted above goes through all the subviews in my view. Jun 23, 2014 at 11:44
  • @Tander your code does not go through all subviews, there can be nested scroll views. Try an recursive method.
    – Tayyab
    Sep 15, 2014 at 11:37

1 Answer 1

10

It seems that you are only iterating through the subviews of your main view. Your UITableView may be nested inside another view. Try doing the following;

//in view did load
[self setScrollToTopFalse:self.view];
self.collectionView.scrollsToTop = YES;

-(void)setScrollToTopFalse:(UIView *)v
{
    for (UIView * v1 in [v subviews]) {
        if ([[v1 class]isSubclassOfClass:[UIScrollView class]]) {
            ((UIScrollView *)v1).scrollsToTop = NO;
        }
        [self setScrollToTopFalse:v1];
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.