开发者

iPhone dev - I want a custom UIView that is made up of many subviews. In what method should I add the subviews?

开发者 https://www.devze.com 2023-03-20 10:28 出处:网络
Basically I want to have a custom UIView that has a bunch of buttons in it. Should I just add the buttons to the view inside initWithFrame? I also would like to set up all their locations in the view

Basically I want to have a custom UIView that has a bunch of buttons in it. Should I just add the buttons to the view inside initWithFrame? I also would like to set up all their locations in the view at the same time. Is there a customary place where all this would be done? I'm guessing I shoul开发者_运维百科dn't do anything in drawRect since I'm not making any custom drawings, just adding buttons as subviews. Thanks.


initWithFrame: is a good place to add the buttons.

If the buttons' frame depends on the parent view's size, you should set the frame of the buttons in the parent view's layoutSubviews method. Otherwise you can just specify the buttons' autoresizingMask when you add the buttons.


You can't set up all their locations "at the same time", but if you call many addSubview: sequentially you don't see even that were added one at a time. You do not always need to subclass UIView(if it is not particularly complex), you can create it at run time, add the components that you want with initWithFrame: and show it; otherwise you can subclass UIView just to create the layout (rounded border, gradient color, etc), initialize it from your class and add the components. If you have some problem, show us more details about your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号