iPhone: Override UIButton buttonWithType to return subclass

后端 未结 3 1193
终归单人心
终归单人心 2021-01-17 17:39

I want to be able to create a UIButton with an oversized responsive area. I know that one way to do that is to override the hitTest method in a subclass, but how do I insta

相关标签:
3条回答
  • 2021-01-17 18:13

    The buttonType property is not used anywhere in UIKit, and in your code you could always check -isKindOfClass:, so overwriting +buttonWithType: for this property is rather pointless IMO. Just use

    return [[[OversizedButton alloc] initWithFrame:...] autorelease];
    

    (You can override the button type with the undocumented method _setButtonType:.)

    0 讨论(0)
  • 2021-01-17 18:20

    Here's how I do it:

    + (instancetype)customButtonWithCustomArgument:(id)customValue {
        XYZCustomButtom *customButton = [super buttonWithType:UIButtonTypeSystem];
        customButton.customProperty = customValue;
        [customButton customFunctionality];
        return customButton;
    }
    

    Works also with other types, UIButtonTypeSystem is just an example.

    0 讨论(0)
  • 2021-01-17 18:25

    UIButton buttonWithType: returns a UIButton or a UIRoundedRectButton, depending on the value of the type parameter.

    As UIButton doesn't provide an initWithType: method, I believe it would be dangerous to try and override buttonWithType:.

    I suggest you subclass UIControl instead. You can then add a button as a subview to your control, and intercept hitTest:withEvent:.

    0 讨论(0)
提交回复
热议问题