完全に制御できる独自の完全にカスタマイズされたボタンを作成するために、カスタム UIButton サブクラスを作成しています。それでも、その UIButton のビューにサブビューを追加しようとすると問題が発生します。サブビューは「タッチ」イベントをブロックするため、UIButton 自体にバブリングしません。
これはデモンストレーションです。まず、フレームを使用して CustomNavigationButton を作成します。マゼンタです。画面にマゼンタが見えるので、そこにあります。次に、その CustomNavigationButton (緑色) にサブビューを追加すると、そこに緑色が表示されますが、緑色の四角形 (サブビュー) をクリックすると、CustomNavigationButton で "UIControlEventTouchUpInside" が呼び出されません。
私のAppDeleteで:
CustomNavigationButton* mapBtn = [[CustomNavigationButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
mapBtn.backgroundColor = [UIColor magentaColor];
[mapBtn addTarget:self action:@selector(goMapHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mapBtn];
そして、ここに私の CustomNavigationButton クラス (UIButton のサブクラス) があります。
@implementation CustomNavigationButton
@synthesize bg;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// STORE INITIAL FRAME
self.initialFrame = frame;
bg = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 40, 40)];
bg.backgroundColor = [UIColor greenColor];
[bg setUserInteractionEnabled:YES];
[self addSubview:bg];
}
return self;
}
@end