gpt4 book ai didi

ios - UICollectionView 内的 WebView 动态内容大小

转载 作者:行者123 更新时间:2023-11-29 05:45:40 24 4
gpt4 key购买 nike

我试图通过引用 collectionViewCell 来调整 UICollectionViewCell 内 WKWebView 动态 HTML 内容的大小。工作正常,但有时在以下行解包可选值时会抛出意外发现的 nil:

let cell:CollectionViewCell = collectionView!.cellForItem(at: indexpath) as! CollectionViewCell

这是我的代码:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
{
let indexpath: IndexPath = IndexPath.init(item: self.pageControl.currentPage, section: 0)
let cell:CollectionViewCell = collectionView!.cellForItem(at: indexpath) as! CollectionViewCell
cell.newsWebView?.evaluateJavaScript("document.readyState", completionHandler: { (result, error) in
if result == nil || error != nil {
return
}
cell.newsWebView?.evaluateJavaScript("document.body.offsetHeight", completionHandler: { (result, error) in
if let height = result as? CGFloat {
cell.webviewHeightConstraint?.constant = height
}
})
CustomeLoader.instance.hideLoader()
}

fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

试试这个。此崩溃可能是由于渲染延迟造成的。

let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline: when) {
let indexpath: IndexPath = IndexPath.init(item: pageControl.currentPage, section: 0)
let cell:CollectionViewCell = collectionView!.cellForItem(at: indexpath) as! CollectionViewCell
cell.newsWebView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
cell.newsWebView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { (height, error) in
cell.webviewHeightConstraint.constant = height as! CGFloat
})
}

})
}

关于ios - UICollectionView 内的 WebView 动态内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56164673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com