Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to top with status bar tap

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

like image 827
Robert J. Clegg Avatar asked Jan 10 '23 08:01

Robert J. Clegg


1 Answers

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];
    }
}
like image 99
Tayyab Avatar answered Jan 26 '23 06:01

Tayyab