Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在哪个观点周期中将子视图添加到 UIView 子类的通用“经验法则”是什么?
例如,如果您决定继承 UITableViewCell 类,您应该在哪个方法中将子视图添加到您的视觉层次结构中?
与我一起工作的其他开发人员,我通常initWithFrame:在layoutSubviews. 尽管有视图,我完全用笔尖布局,通常我的意思是无论如何都要保持固定大小。有时匆忙会胜出,我们在初始化程序中调整它们的大小,特别是对于我们不需要太多调整大小的视图。
initWithFrame:
layoutSubviews
可以这样想:您只需向视图层次结构添加一次。但是您可能需要多次重新定位和调整其大小。因此,您希望在共享该模式的方法中完成这些任务。initWithFrame:只会被调用一次,layoutSubviews很多次。