观察 UIView 的窗口和超级视图属性的变化
我正在寻找一种在从可见视图层次结构中添加或删除通用 UIView 时收到通知的方法。 KVO 看起来是在这种情况下使用的完美工具,但观察视图窗口或超级视图属性的更改不会执行任何操作。对frame或backgroundColor等属性的更改按预期工作,但对与视图层次结构相关的属性的更改似乎从未调用observeValueForKeyPath。
我通过调用automaticallyNotifyObserversForKey来检查UIView是否支持这些属性上的KVO,并且UIView对这两个属性都报告为YES,这让我不知所措。所以我的问题是:
1)有没有办法使用 KVO 来通知与视图层次结构中添加/删除视图相关的事件?
2)如果没有,是否有另一种方法可以通知此类事件,而不涉及子类化 UIView?
I'm looking for a way to be notified when a generic UIView is added or removed from the visible view hierarchy. KVO looked like the perfect thing to use in this case, but observing changes to a view's window or superview properties doesn't do anything. Changes to properties like frame, or backgroundColor work as expected but changed to properties relating to the view hierarchy doesn't seem to ever call observeValueForKeyPath.
I checked to see if UIView supports KVO on those properties by calling automaticallyNotifiesObserversForKey, and UIView reported YES for both, leaving me at a loss. So my questions are:
1) Is there a way to use KVO to be notified of events relating to a view being added/removed to the view hierarchy?
2) If not is there another way to be notified of such events that doesn't involve sub-classing UIView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重写此方法:
您可以在自定义视图中重写这些方法以供其他用途:
Override this method:
And you can override these methods in your custom view for other use:
这是一个方法。很恶心吗?是的。我推荐这种行为吗?不,但我们都是成年人了。
要点是,您使用 method_setImplementation 更改
-[UIView didAddSubview:]
的实现,以便每当调用它时您都会收到通知(并且您可以对willRemoveSubview:
执行相同的操作>)。不幸的是,您将被要求更改所有视图层次结构。您必须添加自己的过滤才能找到您感兴趣的特定视图。要使用,请执行以下操作:
Here is a way. Is it gross? Yes. Do I recommend such behavior? No. But we're all adults here.
The gist is that you use method_setImplementation to change the implementation of
-[UIView didAddSubview:]
so you get notified whenever it's called (and you'd do the same thing forwillRemoveSubview:
). Unfortunately, you will get called for all view hierarchy changes. You'll have to add your own filtering to find the specific views you're interested in.To use, do something like:
基于 @doug-richardson 的代码,为什么不做一些更干净的东西来允许 KVO 的 superview 属性呢?
based on the code by @doug-richardson, why not something a little cleaner that will allow KVO for the superview property?
这是我的解决方案,使用上面的想法,修复了一些错误,并使其可扩展。您可以使用它来监视超级视图随某些类及其子类的 KVO 的变化,它应该是即插即用的。
用 C 语言编写,易于理解且快速。您可以将它们修改为 NSObject 的一些光滑类别。
使用示例:
然后您必须按照正常使用将自己的观察者添加到实例中。
Here's my solution, using ideas from above, fixing some errors, and making it extensible. You can use this flat out to monitor the superview changing with KVO of some class and its subclasses, it should be plug and play.
Written in C to be simple to understand and fast. You could modify these to be some slick categories of NSObject.
Example use:
Then you would have to add your own observer to instances as per normal use.