简体   繁体   English

iphone:在UIWebView上查看?

[英]iphone: View on top of UIWebView?

I'm working on a browser app, and I have an address bar on top the UIWebView. 我正在开发一个浏览器应用程序,我在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. 在MobileSafari上,如果向下滚动,地址栏将开始移动到屏幕外的顶部,UIWebView不会滚动。 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 谢谢

The only way to implement this requires iOS 5. In iOS 5, UIWebView has an UIScrollView subview. 实现这一点的唯一方法是iOS 5.在iOS 5中,UIWebView具有UIScrollView子视图。

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: 使用scrollview委托移动地址栏:

- (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
}

There is a way, but I am not sure if it is a bit too hacky. 有一种方法,但我不确定它是否有点太hacky。 First search for the scrollview within the webview, then alter the contentInset and finally add the searchbar(for example) to the scrollview. 首先在webview中搜索scrollview,然后更改contentInset,最后将搜索栏(例如)添加到scrollview。 The following code is just an example, I did not set any frames correctly and 40 is just a made up height for the searchbar. 以下代码只是一个示例,我没有正确设置任何框架,40只是搜索栏的组合高度。 I am not sure if this will work in every iOS Version. 我不确定这是否适用于每个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];
   }
}

Come to think of it, it is simply a scrolling view with an address bar stuck on the top, and both the web view and the bar always move together. 想想看,它只是一个滚动视图,顶部有一个地址栏,网页视图和条形图总是一起移动。 Now, lets say you create a scroll view and add two subviews, the address bar and the web view (one below the other). 现在,假设您创建一个滚动视图并添加两个子视图,地址栏和Web视图(一个在另一个下面)。 It is to be noted that the height of the web view is determined and fixed after the page has been loaded (in webViewDidFinishLoad: ). 需要注意的是,在加载页面之后(在webViewDidFinishLoad:确定并修复了Web视图的高度。

Hence, it is simply a scrolling view whose contentSize is equal to the height of the bar + the height of the web view. 因此,它只是一个滚动视图,其contentSize等于条形的高度+ Web视图的高度。 Now, by default the web view allows scrolling, as it has a scroll view as a subview. 现在,默认情况下,Web视图允许滚动,因为它具有滚动视图作为子视图。 As only the outer scroll view should be scrolling, it is required that the web view's scrolling be turned off. 由于只有外部滚动视图应滚动,因此需要关闭Web视图的滚动。 For that, fetch the first subview (that's the scroll view) and disable its scrolling using: 为此,获取第一个子视图(即滚动视图)并使用以下命令禁用其滚动:

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

PeakJi's solution works but is a bit laggy. PeakJi的解决方案有效,但有点滞后。 A better solution would be adding an observer to the UIScrollView's content offset, something like 更好的解决方案是在UIScrollView的内容偏移量中添加一个观察者,例如

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

You can find more document on NSKeyValueObserving protocol at 您可以在以下位置找到有关NSKeyValueObserving协议的更多文档

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM