Intercepting didAddSubview

穿精又带淫゛_ 提交于 2020-01-06 20:07:43

问题


I hope this is a simple question. I need to intercept didAddSubview, but will I need to subclass the UIView in order to override that method?

The UIView that I want to override is the UIViewController's view property, so I just want to know how to go about working with this.

Thanks!


回答1:


From Apple UIView documentation (see Methods to Override):

When subclassing UIView, there are only a handful of methods you should override and many methods that you might override depending on your needs. Because UIView is a highly configurable class, there are also many ways to implement sophisticated view behaviors without overriding custom methods, which are discussed in the Alternatives to Subclassing section. In the meantime, the following list includes the methods you might consider overriding in your UIView subclasses:

  • ...
  • didAddSubview:, willRemoveSubview: - Implement these methods as needed to track the additions and removals of subviews.
  • ...

So, create your UIView subclass and override the method. Then, say to your UIViewController that its view will be the one you have subclassed. To achieve this there are a couple of ways:

  • by Xib
  • implementing loadView method and set the view property to be your custom view
  • redifining the view property in viewDidLoad

Hope that helps.



来源:https://stackoverflow.com/questions/11364646/intercepting-didaddsubview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!