我创建了一个简单的测试项目,只包含一个UIWebView,放在填满整个窗口的UIView上。当UIWebView的宽度与UIView相同时,一切都正常。当UIWebView的宽度小于容器的宽度时,水平滚动条会不规则地出现。我加载的网页是一个本地的html文件。它的宽度未设置,应该适合浏览器/UIWebView的宽度。
请帮忙解决。谢谢!
图片如下:
请帮忙解决。谢谢!
图片如下:
![The iPad simulator.](https://istack.dev59.com/k62WW.webp)
method 1. webView.scalePageToFit = YES
, but when the width of the webView changes, the font size of the webpage will change accordingly, to solve this, you can try method 2:
method 2. dynamically set the width of the webView in your Objective-C code, so that when the width of the UIWebView is changed, change the width of the HTML webpage by calling a javascript script.
int webViewWidth = isPortraitOrLandscape ? 768:1024; // the dynamic width of the webView
NSString *javascript = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.width = '%dpx'", webViewWidth];
[webView stringByEvaluatingJavaScriptFromString:javascript];
by doing this, the width of the webView could be matched with the actual HTML webpage.
@"document.getElementsByTagName('body')[0].style.width = '%1.0fpx'"
。 - Felipe Sabinojavascript = [NSString stringWithFormat:@"var viewPortTag=document.createElement('meta'); \
viewPortTag.id='viewport'; \
viewPortTag.name = 'viewport'; \
viewPortTag.content = 'width=%d; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'; \
document.getElementsByTagName('head')[0].appendChild(viewPortTag);" , (int)authWebView.bounds.size.width];
[authWebView stringByEvaluatingJavaScriptFromString:javascript];
您的里程可能会因为其他视口设置的需要而有所不同,或者如果您的网页已经有指定视口的元标记,则可能不需要。
这个JavaScript的想法来自于这个答案:如何在不使用jQuery append的情况下插入元标记?
NSString *javascript = [NSString stringWithFormat:@"document.querySelector('meta[name=\"viewport\"]').setAttribute(\"content\", \"width=%d,initial-scale=1.0,maximum-scale=1.0,user-scalable=0\")", (int)authWebView.bounds.size.width];
- gavdotnet<meta name="viewport" content="width=device-width, user-scalable=no">
。http://caniuse.com/#search=position - Felipe Sabino
<p>
标签,例如:<p>这里是文本...</p>
。 - Xenofex