drawRect not called for custom UIButton subclass when highlighted

后端 未结 3 1019
暖寄归人
暖寄归人 2021-01-03 10:09

When using drawRect for a custom UIButton subclass, it never seems to get called to draw the button when highlighted. Do I need to call setNeedsDisplay for my button in my

3条回答
  •  萌比男神i
    2021-01-03 10:24

    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.

提交回复
热议问题