I have a number of custom UIViews.

I constantly find myself initializing properties in the init of my custom view, but I also set the frame there too.

I usually leave my layoutSubviews empty. If I don't expect my view bounds to change, is it ok to have my various subview frames set in the init itself, or should I move that to layoutSubviews?

I wanted to mention that one of the reasons, I do it this way is because often I find myself having to calculate the frame (size) of my custom view based on how my subviews get laid out.

I usually resize my custom view's frame after all of my subview frame sizes have been set.

有帮助吗?

解决方案

You should avoid allocating/creating your views in layoutSubviews method, because it will be called a lot of times. You can allocate your views in your initializer methods and layout them in layoutSubviews method. But if your views' frames are non-relative to your view's bounds, then there is nothing to worry about setting their frames in your initializer methods.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top