WKWebView 旋转 phone iOS9 后得到旧值 Scroll Height
WKWebView get the old value Scroll Height after rotate the phone iOS9
我有一个单元格内的 WKWebView,但问题是当旋转 phone 时,我得到了 WkWebView 滚动高度的旧值,因此内容无法完全加载。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.contentHeights[3] = self.webView.scrollView.contentSize.height
}
此方法更改 table 单元格的行高,但我得到的值是在旋转之前。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if indexPath.row == 3{
return contentHeights[indexPath.row]
}else{
contentHeights[indexPath.row] = UITableViewAutomaticDimension
return contentHeights[indexPath.row]
}
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 3{
return contentHeights[indexPath.row]
}else{
return UITableViewAutomaticDimension
}
}
第 3 行是 WkWebView 有帮助吗???
我看了好几本post,然后我找到了解决问题的两种方法。
首先我用的是KVO
addObserver(self, forKeyPath: "webView.scrollView.contentSize", options: .New, context: nil)
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "webView.scrollView.contentSize" {
if let nsSize = change[NSKeyValueChangeNewKey] as? NSValue {
let height = nsSize.CGSizeValue().height
// Do something here using content height.
}
}}
removeObserver(self, forKeyPath: "webView.scrollView.contentSize", context: nil)
第二种方式是Javascript上的事件:
将以下代码添加到 html 代码中:
<script>$(document).ready(function(){$( document ).resize(function() {webkit.messageHandlers.callbackHandler.postMessage($( document ).height())});});</script>
添加WkWebView如下配置:
var contentController:WKUserContentController!
override func loadView() {
super.loadView()
contentController = WKUserContentController()
contentController.addScriptMessageHandler(
self,
name: "callbackHandler"
)
config = WKWebViewConfiguration()
config.userContentController = contentController
self.webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: config)
}
添加以下函数:
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
if(message.name == "callbackHandler") {
if !positionOrientationLanscape{
sizeLanscape = CGFloat((message.body as! NSNumber).doubleValue)
contentHeights[4] = sizeLanscape
}else{
sizePortrait = CGFloat((message.body as! NSNumber).doubleValue)
contentHeights[4] = sizePortrait
}
}
}
别忘了deinit
deinit {
self.contentController.removeScriptMessageHandlerForName("callbackHandler")
}
我有一个单元格内的 WKWebView,但问题是当旋转 phone 时,我得到了 WkWebView 滚动高度的旧值,因此内容无法完全加载。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
self.contentHeights[3] = self.webView.scrollView.contentSize.height
}
此方法更改 table 单元格的行高,但我得到的值是在旋转之前。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if indexPath.row == 3{
return contentHeights[indexPath.row]
}else{
contentHeights[indexPath.row] = UITableViewAutomaticDimension
return contentHeights[indexPath.row]
}
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 3{
return contentHeights[indexPath.row]
}else{
return UITableViewAutomaticDimension
}
}
第 3 行是 WkWebView 有帮助吗???
我看了好几本post,然后我找到了解决问题的两种方法。
首先我用的是KVO
addObserver(self, forKeyPath: "webView.scrollView.contentSize", options: .New, context: nil)
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "webView.scrollView.contentSize" {
if let nsSize = change[NSKeyValueChangeNewKey] as? NSValue {
let height = nsSize.CGSizeValue().height
// Do something here using content height.
}
}}
removeObserver(self, forKeyPath: "webView.scrollView.contentSize", context: nil)
第二种方式是Javascript上的事件:
将以下代码添加到 html 代码中:
<script>$(document).ready(function(){$( document ).resize(function() {webkit.messageHandlers.callbackHandler.postMessage($( document ).height())});});</script>
添加WkWebView如下配置:
var contentController:WKUserContentController!
override func loadView() {
super.loadView()
contentController = WKUserContentController()
contentController.addScriptMessageHandler(
self,
name: "callbackHandler"
)
config = WKWebViewConfiguration()
config.userContentController = contentController
self.webView = WKWebView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height), configuration: config)
}
添加以下函数:
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
if(message.name == "callbackHandler") {
if !positionOrientationLanscape{
sizeLanscape = CGFloat((message.body as! NSNumber).doubleValue)
contentHeights[4] = sizeLanscape
}else{
sizePortrait = CGFloat((message.body as! NSNumber).doubleValue)
contentHeights[4] = sizePortrait
}
}
}
别忘了deinit
deinit {
self.contentController.removeScriptMessageHandlerForName("callbackHandler")
}