2

The same behaviour in Safari where the navigation bar scrolls with the web view.

I’ve tried having two scroll views.The first scroll view is the container for the navigation bar and the second scroll view with its scroll indicator hidden. It works (the navigation bar scrolls) almost like the navigation bar in Safari, but here are the issues:

  • The scroll indicator on the second scroll view is hidden, even though I didn't set the property showsScrollIndictor to NO.
  • When I tap the status bar, it doesn't scroll to the top of the first scroll view (nester)
Espresso
  • 4,722
  • 1
  • 24
  • 33

4 Answers4

1

Well, if you don't mind hacking the view tree, just add it as a subview to the WebView's scrollView, and push everything else down. I tried it in Apple's UICatalog (WebViewController.m, instead of [self.view addSubview:urlField];), and it seems to work

UIScrollView * myScrollView;
for (UIView * view in [myWebView subviews]) {
    if ([view isKindOfClass:[UIScrollView class]]) {
    myScrollView = (UIScrollView *) view;  //probably first, but ya never know
    }
}

for (UIView * view in [myScrollView subviews]) {
     CGRect tempFrame = view.frame;
     tempFrame.origin.y  += kTextFieldHeight;
     view.frame = tempFrame;
}
[myScrollView addSubview:urlField];

To make it prettier, you also need to replace "kTweenMargin" with 0 in textFieldFrame and remove the webFrame adjustments:

   webFrame.origin.y += kTopMargin + 5.0;  
   webFrame.size.height -= 40.0; 
mackworth
  • 5,873
  • 2
  • 29
  • 49
  • Hmm. Re-reading the question, looks like you're using Safari only as an example, you don't actually want a webview? Wouldn't the sample principle apply, just put your NavigationBar on the scrollView? Is it that you don't want horizontal scrolling? Then you could implement UIScrollViewDelegate and adjust your NavBar's frame whenever scrolling or Zooming occurs. – mackworth Feb 17 '11 at 21:29
1

Evidently, it's not standard behavior, which means that hacky is probably the only way to go.

You can try to block the scroll view from any interaction except scrolling, passing the touch events into the UIWebView instead. Do this routing in your main controller.

What might also be happening is that Safari is not using a standard toolbar—it could be a bit of HTML that Safari is injecting into the webpage code. UIWebView does have a loadHTML: or similar method; Apple could simply be putting in a bit of Javascript/images to act as a toolbar.

The hardest hack would be to detect how far the user has scrolled in the web view, then move the web view up and the toolbar off the top of the screen by the same amount, until the toolbar is off, then proceed to scroll as normal.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84
  • That could be possible, but how does it shift the scroll indicator when it scrolls to the (top) negative range? That's what I tried accomplishing with nested scroll views. Except for the issue stated above and control is given to the the nester scroll view once and only when the second scroll view has reach the top. – Espresso Feb 23 '11 at 00:26
  • Probably no way around that. The more I look at it, the more it seems that there's some custom solution that Apple's used. You could try drawing your own scroll indicator? It wouldn't be too difficult to have a gray rectangle or stretched ellipse move up and down, and you can "compress" its size as users scroll toward the end…Don't get stuck on duplicating the effect exactly to the pixel. You'll probably run into trouble with the approval guys if you do that anyway. – FeifanZ Feb 23 '11 at 17:54
0

Try making the y value of the nav controller 500 and see if you can scroll it. If you can, then the problem is that it won't scroll into the negative range.

TigerCoding
  • 8,710
  • 10
  • 47
  • 72
  • The indicator is either on or off. There's no way around that as far as I know. [tableView setShowsHorizontalScrollIndicator:NO]; [tableView setShowsVerticalScrollIndicator:NO]; From here: http://stackoverflow.com/questions/821818/is-there-a-way-to-hide-the-scroll-indicators-in-an-uiscrollview – TigerCoding Feb 12 '11 at 21:05
0

Place your UIToolbar and your UIWebView on the same UIScrollView

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • According to Apple documentation, I can't embed a UIWebView to a UIScrollView. And I'm talking about the navigation bar, the bar where you input a web address or a Google search, not the toolbar which has the back/forward buttons. – Espresso Feb 16 '11 at 23:03
  • @Espresso If you can ensure that the contents of the web view can't zoom or scroll (by using the`` tag and ensuring that the view is tall enough), then a `UIWebView` inside of a `UIScrollView` works just fine. The height thing you can do dynamically in the web view delegate using `stringByEvaluatingJavaScriptFromString:` and `clientHeight`. Of course, that's a big _if_. – Daniel Dickison Feb 17 '11 at 20:28