Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone: View on top of UIWebView?

I'm working on a browser app, and I have an address bar on top the UIWebView. On MobileSafari if you scroll down, the address bar starts to move to the top, out of the screen, and the UIWebView doesn't scroll. Only when the address bar disappears completely, it starts to scroll. I would like to have this effect in my app as well.

What's the best way to implement this?

Thanks

like image 202
Alex1987 Avatar asked Feb 23 '11 07:02

Alex1987


People also ask

Does iPhone have WebView?

WKWebView replaces the UIWebView class in iOS 8 and later, and it replaces the WebView class in macOS 10.10 and later.

What is UIWebView on Apple iPhone?

A view that embeds web content in your app.

How do I enable WebView on my iPhone?

iOS Deviceopen the Settings app. go to Safari. scroll down to Advanced. enable the Web Inspector toggle.


1 Answers

The only way to implement this requires iOS 5. In iOS 5, UIWebView has an UIScrollView subview.

And use the following code:

Set a area for the address bar:

[[myWebView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];

Move the address bar using the scrollview delegate:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

        if(scrollView.contentOffset.y>=-64&&scrollView.contentOffset.y<30)
        {
            topBar.frame=CGRectMake(0,-44-scrollView.contentOffset.y, 320, 44);

        }
        else if(scrollView.contentOffset.y<-64)
            topBar.frame=CGRectMake(0,20, 320, 44);//Lock the position
}
like image 183
PeakJi Avatar answered Oct 17 '22 13:10

PeakJi