objective-c UIWebView 不会缩放内容以适应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1511707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIWebView does not scale content to fit
提问by RunLoop
I have a webview which is the top window in the hierarchy and has been declared as shown below. However, it does not scale pages to fit. Pages are top left aligned, but are not scaled, despite the scalesPageToFit property being set to YES. Any help would be greatly appreciated.
我有一个 webview,它是层次结构中的顶部窗口,并已声明如下。但是,它不会缩放页面以适应。页面左上对齐,但不缩放,尽管 scalesPageToFit 属性设置为 YES。任何帮助将不胜感激。
webLookupView = [[UIWebView alloc] initWithFrame:CGRectMake(16, 63, 289, 327)];
webLookupView.backgroundColor = [UIColor lightGrayColor];
webLookupView.dataDetectorTypes = UIDataDetectorTypeAll;
webLookupView.scalesPageToFit = YES;
采纳答案by RunLoop
I have subsequently discovered that some web pages are correctly scaled, but some are not. For web pages which are not properly scaled, javascript can be used to control zooming as follows:
我随后发现有些网页可以正确缩放,但有些则没有。对于未正确缩放的网页,可以使用 javascript 控制缩放,如下所示:
NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
回答by Confused Vorlon
iOS5 gives a better solution
iOS5给出了更好的解决方案
call this from webViewDidFinishLoad
从 webViewDidFinishLoad 调用它
- (void)zoomToFit
{
if ([theWebView respondsToSelector:@selector(scrollView)])
{
UIScrollView *scrollView = [theWebView scrollView];
float zoom = theWebView.bounds.size.width / scrollView.contentSize.width;
scrollView.minimumZoomScale = zoom;
[scrollView setZoomScale:zoom animated:YES];
}
}
回答by vualoaithu
You can use the below code:
您可以使用以下代码:
self.webView.scalesPageToFit = YES;
self.webView.contentMode = UIViewContentModeScaleAspectFit;
in - (void)webViewDidFinishLoad:(UIWebView *)webView methodor viewDidLoad
在- (void)webViewDidFinishLoad:(UIWebView *)webView method或viewDidLoad
回答by Mongus Pong
If you have access to the html you are loading you can chuck the following meta tag in the header :
如果您有权访问正在加载的 html,则可以在标题中添加以下元标记:
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
回答by idris y?ld?z
in Swift2
在 Swift2 中
self.webView.scalesPageToFit = true
self.webView.contentMode = UIViewContentMode.ScaleAspectFit
回答by Rodrigo Gonzalez
As mprivatementions hereyou can update the zoom level of the UIWebView.
正如mprivate在这里提到的,您可以更新 UIWebView 的缩放级别。
- (void)webViewDidFinishLoad:(UIWebView *)theWebView {
CGSize contentSize = theWebView.scrollView.contentSize;
CGSize viewSize = theWebView.bounds.size;
float rw = viewSize.width / contentSize.width;
theWebView.scrollView.minimumZoomScale = rw;
theWebView.scrollView.maximumZoomScale = rw;
theWebView.scrollView.zoomScale = rw;
}
Another way of achieving this would be injecting js code on the webViewDidFinishLoad as RunLoopsays.
实现这一点的另一种方法是在 webViewDidFinishLoad 上注入 js 代码,如RunLoop所说。
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGFloat scale = 0.8; // the scale factor that works for you
NSString *js = [NSString stringWithFormat:@"document.body.style.zoom = %f;",scale];
[webView stringByEvaluatingJavaScriptFromString:js];
}
回答by Zoltan Vinkler
My solution in swift:
我的解决方案迅速:
"Scales pages to fit" on webView needs to be checked in interface builder.
需要在界面构建器中检查 webView 上的“缩放页面以适应”。
Use UIWebViewDelegate.
使用 UIWebViewDelegate。
func webViewDidFinishLoad(webView: UIWebView) {
let zoom = webView.bounds.size.width / webView.scrollView.contentSize.width
webView.scrollView.setZoomScale(zoom, animated: true)
}
回答by crissyg
In xCode 8.3.3, go to the storyboard of the view/scene where the web view is located and check Scale Page to Fit in the attributes inspector section . Also select Scale to Fill as the content mode.
在 xCode 8.3.3 中,转到 web 视图所在的视图/场景的故事板,并在属性检查器部分中选中 Scale Page to Fit 。同时选择缩放填充作为内容模式。
回答by Van
For Swift 3, using RunLoop's effective answer:
对于Swift 3,使用RunLoop的有效答案:
self.webView.stringByEvaluatingJavaScript(from: "document.body.style.zoom = 1.5;")
self.webView.stringByEvaluatingJavaScript(from: "document.body.style.zoom = 1.5;")
回答by Anthony De Souza
Try this in your ViewController's viewDidLoad
在您的 ViewController 的viewDidLoad 中试试这个
[self.myWebView setFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height)];
- I also have the webview set to "scalePageToFit" and viewmode set to "Aspect Fit" in my Storyboard.
- 我还在故事板中将 webview 设置为“scalePageToFit”并将视图模式设置为“Aspect Fit”。

