gontovnik / DGRunkeeperSwitch

Runkeeper design switch control
MIT License
1.88k stars 175 forks source link

KVO doesn't work on iOS 11 #49

Open phoney opened 7 years ago

phoney commented 7 years ago

On iOS 11 there are increased warnings related to KVO and the KVO code in DGRunkeeperSwitch.swift doesn't work. I get this assertion failure [MyTests.TableViewCellTest testOutlets] : failed: caught "NSUnknownKeyException", "[<DGRunkeeperSwitch.DGRunkeeperSwitch 0x7feec8220100> addObserver:<DGRunkeeperSwitch.DGRunkeeperSwitch 0x7feec8220100> forKeyPath:@"selectedBackgroundView.frame" options:1 context:0x0] was sent to an object that is not KVC-compliant for the "selectedBackgroundView" property."

I fixed this problem as follows:

In finishInit I changed to this

selectedBackgroundView.addObserver(self, forKeyPath: "frame", options: .new, context: nil)

In observeValue forKeyPath I changed to this

override open func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if let view = object as? UIView { if keyPath == "frame" && view == selectedBackgroundView { titleMaskView.frame = selectedBackgroundView.frame } } }

and

deinit {
    selectedBackgroundView.removeObserver(self, forKeyPath: "frame")
}
alexanderkhitev commented 7 years ago

@phoney please see https://github.com/gontovnik/DGRunkeeperSwitch/pull/48/files

phoney commented 7 years ago

Thanks. I did look at it and gave it a +1 two weeks ago.

Can it be merged?

alexanderkhitev commented 7 years ago

need to ask @gontovnik

phoney commented 6 years ago

This is fixed by Pull Request #48, which should be merged.

IOSDEVWBA commented 6 years ago

Thank you very much it helped me a lot