What does it mean to call initWithFrame on a UIView class or subclass with CGRectZero?

无人久伴 提交于 2019-12-21 02:43:20

问题


I have seen code that calls initWithFrame of a UIView subclass (such as UILabel) with CGRectZero and things seem to work fine. What does it mean to instantiate a UIView subclass with a 2D point (which seems to be what CGRectZero is)?


回答1:


It just means that you're instantiating the view without an initial value for its frame.

This is done, for instance, when you want to create an instance of the view object and do not need to place it into the view hierarchy right away. Deciding upon and setting the frame can be performed at a later time, using setFrame:.

CGRectZero is commonly used when initializing UITableViewCell's, in SDK 2.x that is. An instance of the view is needed in tableView:cellForRowAtIndexPath:, and there's no need to supply the frame upon creation because the table view will automatically position the cell and makes it the optimal size at a later time.



来源:https://stackoverflow.com/questions/909458/what-does-it-mean-to-call-initwithframe-on-a-uiview-class-or-subclass-with-cgrec

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