Status bar detects taps only when the UIWebView is

2019-06-02 18:29发布

问题:

I have a UIWebView as the root view of a UINavigationController. When a webpage finishes to load, I hide the navigation bar, and I'm looking for a way to show it back. Right now, I'm trying to do that when the user taps the status bar (this approach looks to me more complicated than what I want).

In order to accomplish this, firstly I get the scrollView of the webView:

for (id subview in mainWebView.subviews) {
    if ([[subview class] isSubclassOfClass: [UIScrollView class]]) {
        ((UIScrollView *)subview).delegate = self;
    }
}

and then I use the delegate method:

-(BOOL) scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    return NO;
}

I was hoping that this would work, but here is what happens:

  • Page loads, and navigation bar gets hidden (that's good)
  • I try to tap the status bar, but nothing happens (that's bad)
  • I scroll down so that the webview is "far" from the status bar
  • Without letting the webview go, I tap the status bar with another finger and it works! (that's weird :)

Soooo, any help regarding that? Why is this happening?

回答1:

I just tried this again in iOS 5.0 and it seems to work as expected:

[self.webView.scrollView setDelegate:self];
[self.webView.scrollView setScrollsToTop:YES];

and then:

-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}


回答2:

I had same problem. I solved it by :

[[[webView subviews] objectAtIndex:0] setScrollsToTop:NO];

Also you can get access scrollView property in iOS 5 by :

webView.scrollView.scrollsToTop = NO ;