Iphone: View on top of UIWebView?

I am working on a browser and I have an address bar on top of a UIWebView. On MobileSafari, if you scroll down, the address bar will begin to move to the top of the screen, and UIWebView will not scroll. Only when the address bar completely disappears does it begin to scroll. I would also like to use this effect in my application.

What is the best way to implement this?

thanks

+6
iphone ipad uiscrollview uiwebview
source share
4 answers

The only way to implement this requires iOS 5. In iOS 5, the UIWebView has a subheading called UIScrollView.

And use the following code:

Set the address bar area:

[[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 } 
+15
source share

There is a way, but I'm not sure if it is too hacked. First do a scroll search in the webview, then change the value of the ContentInset and finally add a search bar (e.g.) to the scrollview. The following example is just an example, I did not set the frames correctly, and 40 - only the height of the superscript search bar. I'm not sure if this will work in every version of iOS.

 UIWebView * myWebView = [[UIWebView alloc] init] UISearchBar * mySearchBar = [[UISearchBar alloc] init]; for (NSObject * aSubView in [myWebView subviews]) { if ([aSubView isKindOfClass:[UIScrollView class]]) { UIScrollView * theScrollView = (UIScrollView *)aSubView; theScrollView.contentInset = UIEdgeInsetsMake(40, 0, 0, 0); [theScrollView addSubview:mySearchBar]; } } 
+4
source share

Think about it, it's just a scroll with the address bar stuck on top, and both the web view and the panel always move together. Now let's assume that you create a scroll and add two subzones, an address bar and a web view (one below the other). It should be noted that the height of the web view is determined and fixed after loading the page (in webViewDidFinishLoad: .

Therefore, this is just a scrollable view whose contentSize is equal to the height of the bar + height of the web view. Now, by default, web browsing allows you to scroll, since it has the form of scrolling as a preview. Since you need to scroll only the scroll view, it is required that the scroll of the web view be disabled. To do this, select the first view (this is scrolling) and disable its scrolling using:

 (UIScrollView*)[myWebView.subviews objectAtIndex:0].scrollEnabled = NO; 
0
source share

PeakJi's solution works, but a bit backward. A better solution would be to add an observer to the offset of the contents of the UIScrollView, something like

 [scrollview addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil]; 

An additional document in the NSKeyValueObserving protocol can be found in

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html

0
source share

All Articles