views:

926

answers:

1

When using drawRect for a UIButton subclass, I never seem to get called to draw the button when hiighlighted. Do I need to call setNeedsDisplay for my button in my touch events?

+1  A: 

As far as i can tell there is no straight forward way to subclass UIButton.

UIButton is not the actual class type that is returned by the initializers. UIButton is kind of a front for a series of private classes.

Say you had:

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSLog(@"myButton type: %@", [myButton description]);

You will find the type returned in the log to be "UIRoundedRectButton". The problem with that is you would need to have extended "UIRoundedRectButton". That is not possible as it is a private class which is only ever returned to UIButton.

On top of that "UIRoundedRectButton" is not the only possible returned class all of which are private.

In other words UIButton was built in manner that is not suited to be extended.

abe
Yes, that's sort of what I have discovered. Plus, UIButton doesn't seem to use drawRect to draw itself. It calls my drawRect and then goes ahead and blasts whatever bitmap right over what I draw - and I am not calling the super's drawRect.I have subclassed UIControl instead, although I miss the tap highlighting that UIButton was providing.
mahboudz
its a pain but i dont think apple want us extending the class...wether its cause the class cant handle it or they are forcing use to follow there vision for the structure of the language i just dont know :)
abe