UIScrollView内的iOS UIWebView [英] iOS UIWebView inside a UIScrollView

查看:80
本文介绍了UIScrollView内的iOS UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UIScrollView中包含一个UIWebView. UIWebView有时会超出iPhone屏幕的范围,因此我需要一种滚动视图的方式,以便可以看到所有内容(但我不想使用UIWebView的内置滚动).因此,我正在考虑将所有内容放入UIScrollView中,然后使UIScrollView的高度等于UIWebView及其中其他视图的高度.

I want to have a UIWebView inside a UIScrollView. The UIWebView will sometimes go out of bounds of the iPhone screen so I need a way to scroll the view so I can see all the content (but I don't want to use the built in scrolling of the UIWebView). So I'm thinking of putting all the content inside of a UIScrollView and then making the height of the UIScrollView to equal the height of the UIWebView and other views that are in it.

这是一张有助于描述我的问题的图片:

Here's an image to help describing my problem:

推荐答案

我做的完全一样.这是我的工作方式:

I did exactly the same thing. Here's how I made it work:

  1. 将UIWebView添加为UIScrollView的子视图(显然是;-)
  2. 禁用UIWebView的本地滚动(您可以通过遍历UIWebView的子视图来进行操作,直到找到它的UIScrollView并在其上设置scrollEnabled = NO.
  3. 将UIScrollView的contentSize和UIWebView的框架设置为UIWebViews HTML内容的大小.

最后一点有点棘手,因为您无法确定在UIWebViewDelegate上调用webViewDidFinishLoad:时HTML是否已完全呈现.

The last point is bit tricky because you cannot be sure that the HTML is completely rendered when webViewDidFinishLoad: gets called on the UIWebViewDelegate.

这是获取HTML内容大小的可靠方法:

Here's a reliable way to get the HTML content size:

1.向HTML中添加一个JavaScript函数,以便在HTML文档准备就绪时被调用:

1.Add a javascript function to the HTML that gets called when the HTML Document is ready:

window.onload = function() {
    window.location.href = "ready://" + document.body.offsetHeight;
}

此函数发送URL中具有内容高度的请求.

This functions sends a request that has the content height in it's URL.

2.在UIWebViewDelegate中,您截取此请求:

2.In your UIWebViewDelegate you intercept this request:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        NSURL *url = [request URL];
        if (navigationType == UIWebViewNavigationTypeOther) {
           if ([[url scheme] isEqualToString:@"ready"]) {
               float contentHeight = [[url host] floatValue];
               yourScrollView.contentSize = CGSizeMake(yourScrollView.frame.size.width, contentHeight + yourWebView.frame.origin.y);
               CGRect fr = yourWebView.frame;
               fr.size = CGSizeMake(yourWebView.frame.size.width, contentHeight);
               yourWebView.frame = fr;

               return NO;       
        }

        return YES;
}

希望有帮助

更新

这是Swift 2版本:

Here is the Swift 2 version:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    guard navigationType == .Other else { return true }
    if let url = request.URL, let host = url.host {
        guard url.scheme == "ready" else { return true }
        if let contentHeight = Float(host) {
            yourScrollView.contentSize = CGSizeMake(yourScrollView.bounds.size.width, CGFloat(contentHeight))
            var fr = webView.frame
            fr.size = CGSizeMake(fr.size.width, CGFloat(contentHeight))
            webView.frame = fr
        }

        return false
    }

    return true
}

这篇关于UIScrollView内的iOS UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆