GSK*_*985 5 webview uitableview swift wkwebview
I'm trying to setup a TableView to display my content as follows: 1 [HEADING PHOTO] 2. Content from URL 3. Related items from Json 4. [FOOTER]
Cell 1 is working properly, for cell 2 I have a dynamic height, which gets updated once the content is reloaded. However, if my content (and also the cell) is bigger than my screen it's not show. Only on a zoom gesture the text appears. I can scroll, down, since the Cell and WebView are the proper size. When I scroll back to the top, I the text outside my screen is being cut off again, and appears on zoom gesture.
This is my webview being loaded into my cell
class ArticleDetailPost: UITableViewCell, WKNavigationDelegate, UIScrollViewDelegate {
var articleDetailController = ArticleDetailController(style: .grouped)
var indexPath: IndexPath?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: "cellId")
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var article: Article? {
didSet {
if let bind_id = article?.bind_id {
webView.navigationDelegate = self
webView.scrollView.delegate = self
let base = URL(string: "http://dev.cubrass.nl/app.php?article_id="+bind_id)
let url = URLRequest(url: base!)
webView.load(url)
}
}
}
let webView: WKWebView = {
let wv = WKWebView()
wv.translatesAutoresizingMaskIntoConstraints = false
wv.isOpaque = false
wv.backgroundColor = .clear
wv.scrollView.backgroundColor = .clear
wv.scrollView.isScrollEnabled = false
wv.scrollView.bounces = false
wv.alpha = 0.0
return wv
}()
func setupViews() {
addSubview(webView)
webView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
webView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
}
func viewForZooming(in: UIScrollView) -> UIView? {
return nil;
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { [weak self] (result, error) in
if let height = result as? CGFloat {
let newHeight = height + 200
webView.frame.size.height += newHeight
print("scrollheight ",webView.scrollView.contentSize.height)
print(ArticleDetailController.contentHeights[(self?.indexPath?.row)!])
if (ArticleDetailController.contentHeights[(self?.indexPath?.row)!] != 0.0 &&
ArticleDetailController.contentHeights[(self?.indexPath?.row)!] == newHeight
)
{
webView.alpha = 1.0
return
}
let indexPath = self?.indexPath
ArticleDetailController.contentHeights[(self?.indexPath?.row)!] = newHeight
self?.articleDetailController.tableView.reloadRows(at: [indexPath!], with: .automatic)
}
})
}
}
}
Run Code Online (Sandbox Code Playgroud)
This is my cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ArticleDetailPost
cell.article = article
cell.articleDetailController = self
cell.indexPath = indexPath
cell.selectionStyle = UITableViewCellSelectionStyle.none;
cell.backgroundColor = .clear
return cell
}
Run Code Online (Sandbox Code Playgroud)
寻找解决问题的解决方案,我只需要滚动tableview并查看webview中的所有内容,而无需使用缩放手势进行渲染。
归档时间: |
|
查看次数: |
849 次 |
最近记录: |