iOS中UIWebview中网页宽度自适应的问题
有的網頁中會使用"<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">"這個標簽來設置網頁的寬度,不過帶來的問題是,如果展示這個webview的寬度不等于設備的寬度的時候,就會出現網頁內容過寬左右可以滑動或者網頁左右內容沒有占滿。找了一下,有兩個解決方法:
1. 利用webview中的scrollview的zoom特性,這個方法會讓網頁內容變小
?
- (void)webViewDidFinishLoad:(UIWebView *)theWebView { CGSize contentSize = theWebView.scrollView.contentSize; CGSize viewSize = self.view.bounds.size; float rw = viewSize.width / contentSize.width; theWebView.scrollView.minimumZoomScale = rw; theWebView.scrollView.maximumZoomScale = rw; theWebView.scrollView.zoomScale = rw; }?
?
2. 第二個方法,在客戶端使用js重寫meta標簽,這個也是在webview的delegate的webViewDidFinished回調中調用;我們使用的這種方法來操作,內容不會變小
?
javascript = [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];?
?
參考:
1.?http://stackoverflow.com/questions/10666484/html-content-fit-in-uiwebview-without-zooming-out?
2.?http://stackoverflow.com/questions/5594447/webpage-in-uiwebview-doesnt-autoresize-correctly-when-uiwebviews-width-is-less
3:http://m.blog.csdn.NET/blog/lihei12345/43565859
轉載于:https://www.cnblogs.com/jgCho/p/7458668.html
總結
以上是生活随笔為你收集整理的iOS中UIWebview中网页宽度自适应的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于小程序的Token身份权限体系
- 下一篇: 让威胁管理跟上数据中心奔跑的速度