简体   繁体   中英

Observe layer frame size change in UICollectionViewCell

Is it possible to observe self.layer.frame.size change in UICollectionViewCell ?

I tried to register observer do it like this:

override func awakeFromNib() {
    super.awakeFromNib()
    self.layer.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
}

And then observe like this:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    print(keyPath ?? "NIL")
}

And it came out that observeValue method has been never called.

You can override the var frame and add your function on didSet

override var frame: CGRect {
    didSet {
        //do something
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM