11

I have several view controllers with one or multiple scrollviews. Although I have explicitly set the scrollsToTop flags in view controllers with more than one scroll view, some scroll views refuse to scroll up when I tap the status bar.

After pushing another view controller and popping it the gesture sometimes works in the view it previously hasn't.

It's very confusing and I just don't know what the problem is. How can this issue effectively be debugged? Is there a global (private) notification for the status bar tap so I could scroll the views manually?

2 Answers2

22

I have used code like the following to debug this scenario, before:

- (void)findMisbehavingScrollViews
{
    UIView *view = [[UIApplication sharedApplication] keyWindow];
    [self findMisbehavingScrollViewsIn:view];
}

- (void)findMisbehavingScrollViewsIn:(UIView *)view
{
    if ([view isKindOfClass:[UIScrollView class]])
    {
        NSLog(@"Found UIScrollView: %@", view);
        if ([(UIScrollView *)view scrollsToTop])
        {
            NSLog(@"scrollsToTop = YES!");
        }
    }
    for (UIView *subview in [view subviews])
    {
        [self findMisbehavingScrollViewsIn:subview];
    }
}

Depending on how many UIScrollViews you find, you can modify that code to help debug your particular situation.

Some ideas:

  • Change the background colors of the various scrollviews to identify them on screen.
  • Print the view hierarchy of those scrollviews to identify all of their superviews.

Ideally, you should only find a single UIScrollView in the window hierarchy that has scrollsToTop set to YES.

Sebastian Celis
  • 12,185
  • 6
  • 36
  • 44
1

I changed the accepted answers into swift for convenience:

Swift 5

func findMisbehavingScrollViews() {
    let topView = UIApplication.shared.windows.first { $0.isKeyWindow }
    findMisbehavingScrollViewsIn(topView!)
}

func findMisbehavingScrollViewsIn(_ topView: UIView) {
    if let topView = topView as? UIScrollView {
        if topView.scrollsToTop {
            print("Found UIScrollView: \(topView)")
        }
        for nextView in topView.subviews {
            findMisbehavingScrollViewsIn(nextView)
        }
    }
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168