Ios 错误:已收到但未处理对象:更改:上下文:消息的-observeValueForKeyPath:of
我想根据内容动态设置集合视图的高度,我尝试了以下代码:Ios 错误:已收到但未处理对象:更改:上下文:消息的-observeValueForKeyPath:of,ios,swift,xcode,Ios,Swift,Xcode,我想根据内容动态设置集合视图的高度,我尝试了以下代码: override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(true) self.ItemCollection.addObserver(self , forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil) } func
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.ItemCollection.addObserver(self , forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)
}
func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutableRawPointer) {
let newHeight : CGFloat = self.ItemCollection.collectionViewLayout.collectionViewContentSize.height
var frame : CGRect! = self.ItemCollection.frame
frame.size.height = newHeight
self.ItemCollection.frame = frame
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
self.ItemCollection.removeObserver(self, forKeyPath: "contentSize")
}
我在这行中遇到错误:
self.ItemCollection.addObserver(self , forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)
以下是错误:
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: contentSize
Observed object: <UICollectionView: 0x102911200; frame = (0 275; 375 360); clipsToBounds = YES; autoresize = W+TM+H+BM; gestureRecognizers = <NSArray: 0x1c4247a40>; layer = <CALayer: 0x1c02388a0>; contentOffset: {0, 0}; contentSize: {375, 0}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x102543a80>
Change: {
kind = 1;
old = "NSSize: {20, 360}";
}
Context: 0x0'
An-observeValueForKeyPath:of对象:更改:上下文:已接收但未处理消息。
关键路径:contentSize
观察对象:集合视图布局:
更改:{
种类=1;
old=“NSSize:{20360}”;
}
上下文:0x0'
在Swift 4中,其覆盖功能应为-
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
在Swift 4中,其覆盖功能应为-
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
它是一个覆盖函数,应该是
override func observeValue(forKeyPath-keyPath:String?,of-object:Any?,change:[nskeypaluechangekey:Any]?,context:UnsafeMutableRawPointer?
对于swift 4它起作用了!感谢它的工作,我会让它成为一个覆盖函数,应该是覆盖函数observeValue(forKeyPath-keyPath:String?,of-object:Any?,change:[nskeypaluechangekey:Any]?,context:UnsafeMutableRawPointer?
对于swift 4它工作了!谢谢伙计们,如果成功了,我会让它成为答案