Clip navigation bar to a scroll view
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
toNO
. - When I tap the status bar, it doesn't scroll to the top of the first scroll view (nester)
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;
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.
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.
Place your UIToolbar
and your UIWebView
on the same UIScrollView
精彩评论