관리 메뉴

꿀맛코딩

iOS WKWebView dynamic height (iOS WKWebView 동적 높이) 본문

공부방/iOS

iOS WKWebView dynamic height (iOS WKWebView 동적 높이)

soycrab 2019. 10. 29. 12:42

WKWebView height 를 가져와서

TableView Item의 height를 동적으로 변경 하고 싶다면,

아래의 코드를 사용하면 된다. 

 

var webViewHeight: CGFloat = 40

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in

           if complete != nil {

               webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in

                  self.webViewHeight = webView.scrollView.contentSize.height

               })

           }

    })

}

 

위에서 가져온 높이는 아래처럼 TableView 의 높이 계산에 사용할수 있다. 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    switch headerList[indexPath.section] {

    case .header_content:

        return indexPath.row == 0 ? webViewHeight : 76

    }

}

반응형
Comments