子类化UIView时,只有少数方法
应该覆盖,您可能会依赖的许多方法
您的需求.因为UIView是一个高度可配置的类,所以有
还有很多方法可以实现复杂的视图行为而无需
覆盖自定义方法,这些方法在替代方法"中进行了讨论
子类别部分. 同时,以下列表包括
您可能会考虑在UIView子类中覆盖的方法:
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:
-根据需要实施这些方法,以跟踪子视图的添加和删除.
- ...
- ...
didAddSubview:
, willRemoveSubview:
- Implement these methods as needed to track the additions and removals of subviews.
- ...
因此,创建您的UIView
子类并覆盖该方法.然后,对您的UIViewController
说,它的view
将是您的子类.要实现这一点,有两种方法:
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:
- 通过Xib
- 实施
loadView
方法并将view
属性设置为您的自定义视图
- 重新定义
viewDidLoad
中的view
属性
- by Xib
- implementing
loadView
method and set the view
property to be your custom view
- redifining the
view
property in viewDidLoad
希望有帮助.
这篇关于拦截didAddSubview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!