Espresso
Espresso

Reputation: 4752

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:

Upvotes: 2

Views: 1180

Answers (4)

FeifanZ
FeifanZ

Reputation: 16316

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.

Upvotes: 1

mackworth
mackworth

Reputation: 5953

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; 

Upvotes: 1

WrightsCS
WrightsCS

Reputation: 50707

Place your UIToolbar and your UIWebView on the same UIScrollView

Upvotes: 0

TigerCoding
TigerCoding

Reputation: 8720

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.

Upvotes: 0

Related Questions