設(shè)備測(cè)試: iOS 10
UITableViewCell
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
typedef void(^Block_WebCellChangeHeight)(float);
@interface MCWebCell : UITableViewCell<WKNavigationDelegate>
@property (copy, nonatomic) Block_WebCellChangeHeight Block_WebCellChangeHeight;
@property (strong, nonatomic) WKWebView *webView;
@property(nonatomic, weak) UIScrollView *webScrollView;
@property (assign, nonatomic) float webHeight;
- (void)setModel:(NSString *)model;
@end
#import "MCWebCell.h"
@implementation MCWebCell
- (void)awakeFromNib {
[super awakeFromNib];
_webHeight = 0;
// Initialization code
}
-(UIScrollView *)webScrollView
{
if (!_webScrollView) {
UIScrollView *sv = [[UIScrollView alloc] init];
[self.contentView addSubview:sv];
_webScrollView = sv;
}
return _webScrollView;
}
- (WKWebView *)webView
{
if (!_webView) {
// 配置環(huán)境
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
// 自適應(yīng)屏幕寬度js
NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
// 允許與網(wǎng)頁(yè)交互
configuration.selectionGranularity = NO;
// 自定義配置澈魄,一般用于js調(diào)用oc方法(oc攔截URL中的數(shù)據(jù)做自定義操作)
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
// 添加消息處理,注意:self指代的對(duì)象需要遵守WKScriptMessageHandler協(xié)議榜旦,結(jié)束時(shí)需要移除
//[userContentController addScriptMessageHandler:self name:@"Handle"];
[userContentController addUserScript:wkUserScript];
// 是否支持記憶讀取
configuration.suppressesIncrementalRendering = YES;
// 允許用戶(hù)更改網(wǎng)頁(yè)的設(shè)置
configuration.userContentController = userContentController;
// 創(chuàng)建wk
WKWebView *wv = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
// 設(shè)置背景色
wv.backgroundColor = [UIColor clearColor];
wv.opaque = NO;
// 設(shè)置代理
wv.navigationDelegate = self;
// wv.UIDelegate = self;
// 網(wǎng)頁(yè)內(nèi)容禁用滑動(dòng)
wv.scrollView.scrollEnabled = NO;
// kvo添加進(jìn)度監(jiān)控
// [wv addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:nil];
// 開(kāi)啟手勢(shì)觸摸
wv.allowsBackForwardNavigationGestures = NO;
// 自適應(yīng)
[wv sizeToFit];
[self.webScrollView addSubview:wv];
_webView = wv;
}
return _webView;
}
#pragma mark - <設(shè)置數(shù)據(jù)>
- (void)setModel:(NSString *)model
{
// 手動(dòng)改變圖片適配問(wèn)題尔崔,拼接html代碼后型檀,再加載html代碼
NSString *myStr = [NSString stringWithFormat:@"<head><style>img{max-width:%f !important;}</style></head>", SCREEN_WIDTH-15];
NSString *str = [NSString stringWithFormat:@"%@%@",myStr, model];
[self.webView loadHTMLString:str baseURL:nil];
}
#pragma mark - <WKNavigationDelegate>
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
// 獲取webView的高度
CGFloat webViewHeight = [response floatValue];
// 設(shè)置自定義scrollView的frame
self.webScrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, webViewHeight);
self.webView.frame = CGRectMake(0, 0, SCREEN_WIDTH, webViewHeight);
if (_webHeight != webViewHeight) {
if (_Block_WebCellChangeHeight) {
_Block_WebCellChangeHeight(webViewHeight);
}
_webHeight = webViewHeight;
}
}];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
UIViewController tableView中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return _webHeight;
}
//wkwebView
static NSString *ID = @"MCWebCell";
MCWebCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil] objectAtIndex:0];
}
[cell setModel:_goods.mDescription];
WeakSelf;
cell.Block_WebCellChangeHeight = ^(float height) {
weakSelf.webHeight = height;
[weakSelf.tableView reloadData];
};
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;