How to override -drawrect in UIButton subclass?

As other people have said, you don’t need to subclass UIButton – infact it’s best not to as UIButton is pretty complex (as you found). You have three options:

  • Leave the button’s title blank and add a new text field to the button at the position you want.
  • if you are using 3.0 access the button’s label property. Although the label itself is read only its properties (including frame) are not.
  • Sub-class UIControl. UIControl is a UIView so you can add other views – text and images and it is UIView that implements the action message behaviour (addTarget:action:forControlEvents:). UIButton implements statefulness on top of this.

If you don’t need things like UIButton’s setTitle:forState: and associated functionality but you are using a lot of these controls I would use the third option. Otherwise use the first (or second if you are on 3.0).

Leave a Comment