views:

282

answers:

1

Hi there,

I want to create my own control that will consist of several UILabels and a couple of UITextFields. The problem is I'm not sure where to start! Do I directly subclass UIControl, then create my subviews and add them to the main view in init:? Or do I use layoutSubviews? And will I need to override drawRect:?

I'm used to creating "Controller" classes that will handle adding subviews but if I subclass UIControl then I'm not sure what methods to override to set things up!

I've never done this before so I'd really appreciate a few pointers!

Cheers!

+4  A: 

Are you sure you want UIControl? The UIControl class is intended for fairly simple, typically reusable controls like buttons and text fields that need to support a limited set of events (like "touched" or "value changed"). If you're just trying to create a way to group several views together, you should use UIView instead.

In either case, here's what you should do:

  1. Create your subviews and set most of their properties in -initWithFrame:. Save them in instance variables and add them as subviews of self.

  2. Set their frames in -layoutSubviews, calculating them based on self.bounds. This will be called any time your view changes size, including after -initWithFrame:.

You should not need to implement -drawRect: unless you need to do custom drawing with Core Graphics functions.

Brent Royal-Gordon