1.WKWebView 圖片自適應(yīng)
NSString *js=@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = %f;"
"for(i=0;i"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = %f;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
js = [NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
js = [NSString stringWithFormat:@"%@%@",js,@""];
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
2 .禁止頁面縮放
1.網(wǎng)上查了下資料很多都是通過js user-scalable=no 來實(shí)現(xiàn)的 這樣也可以不過圖片大小在個(gè)別網(wǎng)頁就不自適應(yīng)了,也不知道 怎么與上面的圖片自適應(yīng)代碼組合成一段js 于是就放棄了冻河。愁茁、
NSString *injectionJSString = @"var script = document.createElement('meta');"
"script.name = 'viewport';"
"script.content=\"width=device-width, user-scalable=no\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
- 再次嘗試使用 .wkWebView.scrollView代理方法來關(guān)閉縮放雏门,結(jié)果成關(guān)閉縮放了谴咸,但是圖片又不自適應(yīng)了净刮,靈機(jī)一動(dòng)直接把交互性給關(guān)了了谓晌,然后就好了具温。蚕涤。。就只設(shè)置了 1的圖片自適應(yīng)铣猩。另外吧wkWebView的交互給關(guān)閉了揖铜。
self.wkWebView.userInteractionEnabled = NO;
全部代碼
- (void)configWKWebView{
// NSString *js = @" $('meta[name=description]').remove(); $('head').append( '' );";
NSString *js=@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth = %f;"
"for(i=0;i"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = %f;"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
js = [NSString stringWithFormat:js,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.width-15];
js = [NSString stringWithFormat:@"%@%@",js,@""];
WKUserScript *script = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
NSString *injectionJSString = @"var script = document.createElement('meta');"
"script.name = 'viewport';"
"script.content=\"width=device-width, user-scalable=no\";"
"document.getElementsByTagName('head')[0].appendChild(script);";
WKUserScript *injectionJSStringScript = [[WKUserScript alloc] initWithSource:injectionJSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
WKUserContentController *userController = [WKUserContentController new];
NSMutableString *javascript = [NSMutableString string]; [javascript appendString:@"document.documentElement.style.webkitTouchCallout='none';"];//禁止長按
[javascript appendString:@"document.documentElement.style.webkitUserSelect='none';"];//禁止選擇
WKUserScript *noneSelectScript = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKWebViewConfiguration *config =[[WKWebViewConfiguration alloc]init];
config.preferences = [WKPreferences new];
config.preferences.minimumFontSize = 10;
config.preferences.javaScriptEnabled = YES;
config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
[userController addUserScript:script];
[userController addUserScript:noneSelectScript];
[userController addUserScript:injectionJSStringScript];
config.userContentController = userController;
self.wkWebView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
self.wkWebView.scrollView.scrollEnabled = NO;
self.wkWebView.scrollView.bounces = NO;
self.wkWebView.userInteractionEnabled = NO;
// self.wkWebView.navigationDelegate = self;
// self.wkWebView.scrollView.delegate = self;
//監(jiān)聽wekwebview 的高度來技術(shù) tableview Cell 的高度
[self.wkWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
/** 防止?jié)L動(dòng)一直刷新,出現(xiàn)閃屏 */
if ([keyPath isEqualToString:@"contentSize"]&&self.wkWebView.scrollView == object) {
// NSLog(@"----%f",self.wkWebViewHeight);
if (self.wkWebViewHeight < self.wkWebView.scrollView.contentSize.height &&self.isMoreWebView) {
//如果點(diǎn)擊的展開就一直刷新 达皿,指導(dǎo)拿到最終高度
self.wkWebViewHeight= self.wkWebView.scrollView.contentSize.height;
self.cellGoodsDetailsHeight =self.wkWebViewHeight;
dispatch_async(dispatch_get_main_queue(), ^{
// UI更新
NSLog(@"-----刷新tableview---%f",self.wkWebViewHeight);
[self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
});
}
//記錄wkWebview 內(nèi)容的高度 點(diǎn)擊展開的時(shí)候 直接刷新
self.wkWebViewHeight= self.wkWebView.scrollView.contentSize.height;
}
}