bitwit
bitwit

Reputation: 2676

UIScrollView scrollViewShouldScrollToTop: alternative available when already at the top?

According to the official UIScrollView documentation related to scrollsToTop:

If that scroll view has scrollsToTop set to NO, its delegate returns NO from scrollViewShouldScrollToTop:, or the content is already at the top, nothing happens.

So, as a result, the delegate method scrollViewShouldScrollToTop: is not fired when the scrollview is at the top when I tap the status bar. However, I'm trying to take advantage of this call to programatically make my own decision about which scrollview in the hierarchy needs to scroll.

So what is the best alternative to this? I'm trying to find a way to catch taps on the status bar more than anything. Based on what I've read it sounded like this was the best way to catch the call and handle it appropriately.

EDIT: The next best alternative I could think of was to place a clear UIView with a UITapGestureRecognizer over top of the status bar via a different UIWindow that sits on top.

Upvotes: 3

Views: 1420

Answers (1)

Pauls
Pauls

Reputation: 2636

Since iOS7 controller views go by default underneath statusBar and navigationBars.

You can take advantage of this, and add a TapGestureRecognizer to your VC´s main view. In the tap delegate method check whether the tapped point in view belongs to the status bar frame.

- (void)userTappedOnView:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint tapLocation = [recognizer locationInView:self.view];
        CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
        if (CGRectContainsPoint(statusBarFrame, point)) {
            //Scroll other views to top here..
        }
    }
}

Upvotes: 1

Related Questions