如何确定UIWebView的内容大小? [英] How to determine the content size of a UIWebView?

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

问题描述

我有一个UIWebView具有不同的(单页)内容。我想找出的内容的CGSize来适当调整我的父视图的大小。显然 -sizeThatFits:不幸的是只返回webView的当前帧大小。

I have a UIWebView with different (single page) content. I'd like to find out the CGSize of the content to resize my parent views appropriately. The obvious -sizeThatFits: unfortunately just returns the current frame size of the webView.

推荐答案

原来,我的第一个猜测使用 -sizeThatFits:没有完全错误。它似乎工作,但只有当webView的框架设置为最小尺寸,然后发送 -sizeThatFits:。之后,我们可以通过拟合大小来纠正错误的帧大小。这听起来可怕,但它实际上不是那么糟糕。由于我们同时进行框架更改,因此视图不会更新,也不会闪烁。

It turned out that my first guess using -sizeThatFits: was not completely wrong. It seems to work, but only if the frame of the webView is set to a minimal size prior to sending -sizeThatFits:. After that we can correct the wrong frame size by the fitting size. This sounds terrible but it's actually not that bad. Since we do both frame changes right after each other, the view isn't updated and doesn't flicker.

当然,我们必须等待内容因此我们将代码放入 -webViewDidFinishLoad:委托方法中。

Of course, we have to wait until the content has been loaded, so we put the code into the -webViewDidFinishLoad: delegate method.

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height);
}

我应该指出另一种方法(感谢@GregInYEG)。不确定哪个解决方案效果更好。

I should point out there's another approach (thanks @GregInYEG) using JavaScript. Not sure which solution performs better.

我喜欢这两个黑客解决方案。

Of two hacky solutions I like this one better.

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

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