WKWebView 컴퓨팅 높이 및 페이지 크기 조정

3630 단어
다음 코드는cell에 WK 계산 높이를view Controller에 되돌려주고tableView를 새로 고칩니다
  • tableViewCell 코드
  • KVO를 통해 웹View 고도의 변경 사항을 수신합니다:wkWebView.scrollView.scrollEnabled = NO; 스크롤 속성을 NO로 설정합니다. 그렇지 않으면 높이가 정확하지 않을 수 있습니다. 높이가 되돌아온 후에 감청을 제거하십시오. 그렇지 않으면 중복 호출됩니다.
    @interface CdConferenceGeneralizeCell () 
    
    @end
    @implementation CdConferenceGeneralizeCell
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            self.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        return self;
    }
    - (void)configuration:(CdConferenceDetailModel *)model{
        [self.wkWebView loadHTMLString:model.meetingRemark.introduce baseURL:nil];
    }
    #define contentSizeKey @"contentSize"
    
    - (WKWebView *)wkWebView{
        if (!_wkWebView) {
            //        
            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;
            _wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, HHBWIDTH, 0) configuration:wkWebConfig];
            _wkWebView.scrollView.scrollEnabled = NO;
            _wkWebView.navigationDelegate = self;
            [_wkWebView.scrollView addObserver:self forKeyPath:contentSizeKey options:NSKeyValueObservingOptionNew context:nil];
            [self.contentView addSubview:_wkWebView];
        }
        return _wkWebView;
    }
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
        return nil;
    }
    - (void)removeObserverForWebViewContentSize {
        
        [_wkWebView.scrollView removeObserver:self forKeyPath:contentSizeKey];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([keyPath isEqualToString:contentSizeKey]) {
            CGFloat height = self.wkWebView.scrollView.contentSize.height;
            if (height > 0) {
                if (_wkWebViewHeight) {
                    [self removeObserverForWebViewContentSize];//   :           ,           
                    _wkWebView.frame = CGRectMake(0, 0, HHBWIDTH, height);
                    self.wkWebViewHeight(height);
                }
            }
        }
    }
    
    + (NSString *)identifier{
        return NSStringFromClass([self class]);
    }
    
    @end
    
    
  • viewController 코드
  •  CdConferenceGeneralizeCell *cell = [tableView dequeueReusableCellWithIdentifier:[CdConferenceGeneralizeCell identifier]];
            if (!cell) {
                cell = [[CdConferenceGeneralizeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[CdConferenceGeneralizeCell identifier]];
            }
            WS(weakSelf);
            [cell configuration:_allMeetDetailModel];
            cell.wkWebViewHeight = ^(CGFloat height) {
                _webViewH = height;
                [weakSelf.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
            };
            return cell;
            ```
    
    

    좋은 웹페이지 즐겨찾기