【发布时间】:2012-12-11 13:27:13
【问题描述】:
我正在编写一个自定义 UIButton 子类,以创建我自己的完全自定义按钮 拥有完全的控制权。仍然,当我尝试添加子视图时遇到问题 到那个 UIButton 的视图。子视图阻止“触摸”事件,因此它不会冒泡 UIButton 本身..
这是一个演示: 我首先用一个框架创建了我的 CustomNavigationButton。它是洋红色的。 我可以在屏幕上看到洋红色,所以它就在那里。其次,我添加了一个子视图 到那个 CustomNavigationButton(它是绿色的),我可以看到绿色,所以它是 那里,但如果我点击绿色矩形(子视图),“UIControlEventTouchUpInside” 没有在我的 CustomNavigationButton 上被调用..
在我的 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
【问题讨论】:
标签: ios uibutton subview user-interaction