iOS 混合开发 —— WebView 问题技巧

 

1、图片太大

1
2
3
4
5
6
7
8
9
10
11
12
13
- (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()"];
}

  

2、内容高度

通过JS来获取高度:document.body.offsetHeight;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
  CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];
  if (self.webViewHeight != height && self.count <= 3) {
    self.webViewHeight = height;
    self.count++;
     
    [self updateUI];
  }
}
 
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  self.webViewHeight = webView.scrollView.contentSize.height;
  return YES;
}

  

github地址: https://github.com/lc081200/hybirdApp 

posted on   iRemark  阅读(421)  评论(0编辑  收藏  举报

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示