---
title: "WebView图片自适应屏幕"
author: "啸笑天"
date: "2016-03-16T01:09:46Z"
tags: []
categories: ["移动开发"]
slug: "webview"
draft: false
lastmod: "2016-03-16T01:09:46Z"
id: 2283315
url: "https://justsee.iteye.com/blog/2283315"
---

```
- (void)webViewDidFinishLoad:(UIWebView *)webView {

  NSString *js = @"function imgAutoFit() { \

     var imgs = document.getElementsByTagName('img'); \

     for (var i = 0; i < imgs.length; ++i) {\

        var img = imgs[i];   \

        img.style.maxWidth = %f;   \

     } \

  }";

  js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];

 

  [webView stringByEvaluatingJavaScriptFromString:js];

  [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];

}
```