起因
對(duì)于展示新聞詳情界面,一開(kāi)始試過(guò)UIWebView、UITextView,
UITextView感覺(jué)不夠強(qiáng)大,不過(guò)是可以展示的
UIWebView在網(wǎng)上搜索一番之后,蘋果逐漸在淘汰它,缺點(diǎn)很多,所以我也沒(méi)用它了,雖然能正常實(shí)現(xiàn)功能
所以最后決定用WKWebView
過(guò)程
問(wèn)題 1:圖片大小和字體大小
解決:這可以通過(guò)注入js代碼來(lái)顯示,經(jīng)過(guò)幾番周折是能很好的實(shí)現(xiàn)
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *wkUController = [[WKUserContentController alloc] init];
[wkUController addUserScript:wkUScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = wkUController;
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1) configuration:wkWebConfig];
問(wèn)題 2:GIF動(dòng)圖
出現(xiàn)這個(gè)問(wèn)題可把我折磨得夠嗆,我估摸著有一個(gè)星期
先說(shuō)說(shuō)界面構(gòu)架吧:
- 一個(gè)空的 UIViewController
- 一個(gè)全局 CGFloat webViewHeight(通過(guò)WKWebView的內(nèi)容高度得到)
- 在上面添加一個(gè) UITableView:有3個(gè)section
- 第一個(gè)section只有一個(gè)cell,展示新聞的標(biāo)題、來(lái)源、作者
- 第二個(gè)section只有一個(gè)cell,展示新聞的主要內(nèi)容,這個(gè)cell里加入了一個(gè)UIScrollView,UIScrollView里面加入了WKWebView,WKWebView里展示新聞的內(nèi)容
- 第三個(gè)section的cell是動(dòng)態(tài)的,是用來(lái)顯示評(píng)論內(nèi)容
最后就是UIViewController的底部添加一個(gè)輸入框用來(lái)評(píng)論
WKWebView 內(nèi)容的展示是通過(guò)KVO來(lái)實(shí)現(xiàn)的
問(wèn)題:
當(dāng)新聞內(nèi)容有GIF動(dòng)圖的時(shí)候,界面無(wú)法滑動(dòng),不是固定死的那種無(wú)法滑動(dòng),而是不聽(tīng)話的無(wú)法滑動(dòng)或者說(shuō)劃不動(dòng),GIF一直在閃(在此時(shí)我是奔潰的,查詢了關(guān)于WKWebView和HTML的資料,都無(wú)法解決)
解決:
然后我就使勁上下滑動(dòng),就不信滑不上去(事實(shí)證明,通過(guò)這樣的測(cè)試可以發(fā)現(xiàn)很多問(wèn)題,并且也逐漸找到了問(wèn)題的原因,讓真相浮出水面)
點(diǎn)擊 Debug View Hierarchy 后
看見(jiàn)section0 的cell 有覆蓋現(xiàn)象,加幾句代碼輕松解決
UITableViewCell *defaultCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];
if (defaultCell == nil){
defaultCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath];
}else {
while ([defaultCell.contentView.subviews lastObject]!= nil) {
[(UIView *)[defaultCell.contentView.subviews lastObject] removeFromSuperview];
}
}
問(wèn)題:
然后我對(duì)比了正常img的新聞和GIF的新聞,發(fā)現(xiàn):正常img的新聞加載完之后kvo調(diào)用之后會(huì)自己停下來(lái),但是GIF的新聞一直在調(diào)用kvo是根本停不下來(lái)??!
解決:
雖然不能確定就是GIF圖片造成它的內(nèi)容一直變動(dòng),但最后我還是換成了在 didFinishNavigation 方法里獲取高度 刷新界面
[webView evaluateJavaScript:@"Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)"
completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (!error) {
NSNumber *height = result;
self.webViewHeight = [height floatValue] + 30;
webView.frame = CGRectMake(0, 0, self.view.frame.size.width, [height floatValue]);
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, [height floatValue]);
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, [height floatValue]);
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:1], nil]withRowAnimation:UITableViewRowAnimationNone];
}
}];
問(wèn)題:
沒(méi)錯(cuò),我成功了,但也不是很舒服
原因是,section2的cell在第一次向上滑動(dòng)的時(shí)候是對(duì)于自身滑動(dòng)的,不是對(duì)于tableView滑動(dòng)的,現(xiàn)象就是新聞標(biāo)題那個(gè)cell不動(dòng),但撒手之后再次滑動(dòng)就是跟著tableView滑動(dòng),我覺(jué)得這不科學(xué)
解決:
加了兩句代碼:
WebView.scrollView.scrollEnabled = NO;
self.scrollView.scrollEnabled = NO;
這下子就舒服了,我想應(yīng)該還會(huì)有更好的展示方式或是解決方法,知識(shí)的道路上不斷探索吧!