UITableView中加載WKWebView客税,高度計算問題

一般情況下在UITableView的header或cell中加載webview况褪,需要動態(tài)計算webview的內(nèi)容高度,然后刷新tableview更耻,那么接下來問題的重點就是如何準確的拿到webview的內(nèi)容高度了

webview加載分兩種:1测垛、加載靜態(tài)HTML,2酥夭、加載動態(tài)HTML赐纱。
兩者在高度計算上是有區(qū)別的,下面就針對這兩種進行討論熬北。

一疙描、加載動態(tài)HTML

[ 思路1:從WKWebview的加載完成代理方法中著手]

代碼如下:

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result,NSError * _Nullable error) {
        //1.獲取頁面高度,并重新計算webview的高度
        
        //2.緩存頁面高度

       //3.tableview update 
    }];
}

但是讶隐,很可惜,這里獲取的高度有時不精確起胰,于是,采用了另一種方法。

[ 思路2:使用KVO監(jiān)聽webView.scrollView的contentSize]

code:
cell的方法

  • cell初始化方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIView *superView = self.contentView;
        superView.backgroundColor = [UIColor blueColor];
        WKWebView *webView = [[WKWebView alloc] initWithFrame:superView.bounds];
        webView.scrollView.scrollEnabled = NO;//禁用webView滑動
        webView.scrollView.userInteractionEnabled = NO;
//自適應(yīng)寬高效五,這句要加
        webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
       // webView.navigationDelegate = self;
        _webView = webView;
        [superView addSubview:_webView];
    //監(jiān)聽webView.scrollView的contentSize屬性
        [_webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}
  • KVO監(jiān)聽處理
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentSize"]) {
        __weak typeof(self) weakSelf = self;
//執(zhí)行js方法"document.body.offsetHeight" 地消,獲取webview內(nèi)容高度
        [_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
            CGFloat contentHeight = [result floatValue];
            if (weakSelf.webHeightChangedCallback) {
                weakSelf.webHeightChangedCallback(contentHeight);
            }
        }];
    }
}
  • 移除kvo監(jiān)聽
- (void)dealloc
{
    [_webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}
  • tableview部分
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        return _webHeight;//web高度,全局屬性
    }
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
        WebTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(WebTableViewCell.class)];
        if (!cell) {
            cell = [[WebTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(WebTableViewCell.class)];
        }
        __weak typeof(self) weakSelf = self;
        cell.webHeightChangedCallback = ^(CGFloat webHeight) {
            __strong typeof(self) strongSelf = weakSelf;
            if (strongSelf->_webHeight < webHeight) {//當緩存的高度小于返回的高度時畏妖,更新緩存高度脉执,刷新tableview
                strongSelf->_webHeight = webHeight;
                [weakSelf.tableView beginUpdates];
                [weakSelf.tableView endUpdates];
            }
        };
        return cell;
    }
    return nil;
}

//去除web頁底部空白
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 判斷webView所在的cell是否可見,如果可見就layout
    NSArray *cells = self.tableView.visibleCells;
    for (UITableViewCell *cell in cells) {
        if ([cell isKindOfClass:[WebTableViewCell class]]) {
            WebTableViewCell *webCell = (WebTableViewCell *)cell;
            [webCell.webView setNeedsLayout];
        }
    }
}

二戒劫、加載靜態(tài)HTML

和動態(tài)HTML類似半夷,需要監(jiān)聽高度

 [_webView.scrollView addObserver:self forKeyPath:contentSizeKey options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

懶加載webview

- (WKWebView *)webView{
    if (!_webView) {
        //以下代碼適配大小
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        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;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 0) configuration:wkWebConfig];
        //禁用scrollview的交互
        _webView.scrollView.scrollEnabled = NO;
        _webView.scrollView.userInteractionEnabled = NO;
        _webView.navigationDelegate = self;
        _webView.scrollView.delegate = self;
       
        [_webView addSubview:self.indicatorView];
        //高度監(jiān)聽
        [_webView.scrollView addObserver:self forKeyPath:contentSizeKey options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        [self.contentView addSubview:_webView];
    }
    return _webView;
}

加載HTML

- (void)configuration:(NSString *)html{
    if (html.length > 0 && ([html containsString:@"http"] || [html containsString:@"https"])) {
        [self.webView loadHTMLString:html baseURL:nil];
    }
}

禁止縮放

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return nil;
}
//監(jiān)聽移除
- (void)removeObserverForWebViewContentSize {
    [_webView.scrollView removeObserver:self forKeyPath:contentSizeKey];
}

//KVO獲取高度
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:contentSizeKey]) {
        CGFloat height = self.webView.scrollView.contentSize.height;
        id old = change[NSKeyValueChangeOldKey];
        id new = change[NSKeyValueChangeNewKey];
//        NSLog(@"height-->%f->old-->%@-->new-->%@",height,old,new);
        if (height > 0) {
            if (_wkWebViewHeight) {
                _webView.frame = CGRectMake(0, 0, ScreenW, height);
                self.indicatorView.center = _webView.center;
                self.wkWebViewHeight(height);
            }
        }
        if ([old isEqual:new]) {
            [self removeObserverForWebViewContentSize];//重點 : 高度回調(diào)后把監(jiān)聽移除,不然會重復(fù)走這段代碼
            self.loadFinished();
        }
    }
}

//當webview加載完成迅细,再次更新高度
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    NSLog(@"加載結(jié)束didFinishNavigation");
    [self.indicatorView stopAnimating];
    CGFloat webH = webView.scrollView.contentSize.height;
    _webView.frame = CGRectMake(0, 0, ScreenW, webH);
    if (_wkWebViewHeight) {
        self.wkWebViewHeight(webH);
        self.loadFinished();
    }
}

controller里更加獲取的高度刷新tableview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
···
      cell.wkWebViewHeight = ^(CGFloat height) {
            if (weakSelf.webViewH != height) {
                weakSelf.webViewH = height;
                [weakSelf.tableView beginUpdates];
                [weakSelf.tableView endUpdates];
            }
            
        };
        cell.loadFinished = ^{
            [weakSelf.tableView beginUpdates];
            [weakSelf.tableView endUpdates];
        };
···
}

以上僅為本人在開發(fā)中遇到的問題巫橄,并嘗試解決,如有問題茵典,歡迎指正湘换,謝謝!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末统阿,一起剝皮案震驚了整個濱河市彩倚,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌扶平,老刑警劉巖署恍,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蜻直,居然都是意外死亡,警方通過查閱死者的電腦和手機袁串,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門概而,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人囱修,你說我怎么就攤上這事赎瑰。” “怎么了破镰?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵餐曼,是天一觀的道長。 經(jīng)常有香客問我鲜漩,道長源譬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任孕似,我火速辦了婚禮踩娘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘喉祭。我一直安慰自己养渴,他們只是感情好雷绢,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著理卑,像睡著了一般翘紊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上藐唠,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天帆疟,我揣著相機與錄音,去河邊找鬼中捆。 笑死鸯匹,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的泄伪。 我是一名探鬼主播殴蓬,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蟋滴!你這毒婦竟也來了染厅?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤津函,失蹤者是張志新(化名)和其女友劉穎肖粮,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體尔苦,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡涩馆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了允坚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片魂那。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖稠项,靈堂內(nèi)的尸體忽然破棺而出涯雅,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站猜煮,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏蔗候。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一挤土、第九天 我趴在偏房一處隱蔽的房頂上張望琴庵。 院中可真熱鬧,春花似錦、人聲如沸迷殿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽庆寺。三九已至蚊夫,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間懦尝,已是汗流浹背知纷。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留陵霉,地道東北人琅轧。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像踊挠,于是被迫代替她去往敵國和親乍桂。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容