iPhone:覆盖UIButton buttonWithType以返回子类

Ama*_*mer 10 iphone subclass uibutton

我希望能够创建一个具有超大响应区域的UIButton.我知道一种方法是覆盖子类中的hitTest方法,但是如何首先实例化我的自定义按钮对象?

[OversizedButton buttonWithType: UIButtonTypeDetailDisclosure];
Run Code Online (Sandbox Code Playgroud)

因为buttonWithType返回一个UIButton而不是OversizedButton,所以它不会开箱即用.

所以我似乎也需要覆盖buttonWithType方法.有谁知道如何做到这一点?

@implementation OversizedButton

+ (id)buttonWithType:(UIButtonType)buttonType
{
   // Construct and return an OversizedButton rather than a UIButton
   // Needs to handle special types such as UIButtonTypeDetailDisclosure
   // I NEED TO KNOW HOW TO DO THIS PART
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{
   // Return results if touch event was in oversized region
   // I ALREADY KNOW HOW TO DO THIS PART
}

@end
Run Code Online (Sandbox Code Playgroud)

或者,也许我可以使用alloc/initWithFrame创建按钮.但是buttonType属性是readonly,那么如何创建自定义按钮类型?

注意:我知道还有其他方法可以做到这一点,比如在可见的后面有一个隐形按钮.我不关心这种方法,宁愿避免它.对上述方法的任何帮助都会非常有帮助.谢谢

ken*_*ytm 0

buttonType属性在 UIKit 中的任何地方都没有使用,并且在您的代码中您可以随时检查-isKindOfClass:,因此+buttonWithType:在 IMO 中覆盖该属性是毫无意义的。只需使用

return [[[OversizedButton alloc] initWithFrame:...] autorelease];
Run Code Online (Sandbox Code Playgroud)

(您可以使用未记录的方法覆盖按钮类型_setButtonType:。)

  • 您是否认真建议我在需要 Apple 批准的商业应用程序中使用未记录的方法? (7认同)