UIScrollView 内的 iOS UIWebView

iOS UIWebView inside a UIScrollView(UIScrollView 内的 iOS UIWebView)

本文介绍了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 内容的大小.

最后一点有点棘手,因为当 webViewDidFinishLoad: 在 UIWebViewDelegate 上被调用时,您无法确定 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 文档准备好时调用的 HTML 中添加一个 javascript 函数:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIScrollView 内的 iOS UIWebView

基础教程推荐