UIWebView 不会调整大小以适应内容?

发布于 2024-11-03 09:04:01 字数 763 浏览 1 评论 0原文

我的 webView 在完成加载后无法正确调整大小。我已经尝试过这段代码。

- (void)webViewDidFinishLoad:(UIWebView *)webview {
    CGRect frame = webView.frame;
    frame.size.height = 1;
    frame.size.width = screenWidth;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    [bodyScroll setContentSize:CGSizeMake(screenWidth, webView.frame.origin.y + webView.frame.size.height+keyboardRectY+60)];

它增加了超出 screenWidth 的宽度,因此我的内容水平扩展超出了我不想要的视图大小。

改变

CGSize fittingSize = [webView sizeThatFits:CGSizeMake(screenWidht, 1)];

也会导致同样的情况。

我怎样才能摆脱这个? webview 宽度应始终保持为屏幕宽度,只有其高度应根据内容大小进行调整,以便 webview 几乎适合整个内容。

提前致谢

My webView is not resizing properly after it finished loading. I have tried this code.

- (void)webViewDidFinishLoad:(UIWebView *)webview {
    CGRect frame = webView.frame;
    frame.size.height = 1;
    frame.size.width = screenWidth;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    [bodyScroll setContentSize:CGSizeMake(screenWidth, webView.frame.origin.y + webView.frame.size.height+keyboardRectY+60)];

It is incresing the width beyond the screenWidth and hence my content horizontally expands beyond the view size which I don't want.

changing

CGSize fittingSize = [webView sizeThatFits:CGSizeMake(screenWidht, 1)];

also leads to the same situation.

How can I get rid of this? The webview width should always remain as screenwidth and only it's height should adjust based on the content size so that the webView just about fits the whole content.

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-11-10 09:04:01

设置 frame 的大小时,您将覆盖之前设置的宽度。这应该是:

...
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
fittingSize.width = screenWidth; // Add this
frame.size = fittingSize
...

When setting the size of frame, you are overwriting the width you set previously. This should it:

...
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
fittingSize.width = screenWidth; // Add this
frame.size = fittingSize
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文