[英]addTarget doesn't work for UIButtons in iOS, view covered up
I have two UIButtons I am creating programmatically and setting targets. 我有两个UIButtons我正在以编程方式创建并设置目标。 If I add these buttons to a main view, everything works beautifully.
如果我将这些按钮添加到主视图中,一切都可以很好地工作。 However when I add the to a subview first and add this subview to the main view, touch events don't work.
但是,当我首先添加到子视图并将此子视图添加到主视图时,触摸事件不起作用。 I'm guessing these buttons are being covered up.
我猜这些按钮被掩盖了。
How can I uncover these buttons while keeping them inside the subview? 如何在将这些按钮保留在子视图中的同时将其打开?
Here is my code: 这是我的代码:
// Create button panel here
UIImage *buttonPanel = [UIImage imageNamed:@"left_button_background"];
UIImageView *buttonPanelView = [[UIImageView alloc] initWithImage:buttonPanel];
[buttonPanelView setFrame:CGRectMake(20, 186, 363, 345)];
// Populate the panel with buttons
UIButton *cmsButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 351, 142)];
[cmsButton setImage:[UIImage imageNamed:@"BTN_MATLIB"] forState:UIControlStateNormal];
[cmsButton addTarget:self action:@selector(loadCMS) forControlEvents:UIControlEventTouchDown];
[buttonPanelView addSubview:cmsButton];
[self setCms:cmsButton];
UIButton *calcButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 196, 351, 142)];
[calcButton setImage:[UIImage imageNamed:@"BTN_MORE_RESOURCES"] forState:UIControlStateNormal];
[calcButton addTarget:self action:@selector(loadCalc) forControlEvents:UIControlEventTouchDown];
[buttonPanelView addSubview:calcButton];
[self setCalc:calc];
[[self view] addSubview:buttonPanelView];
默认情况下,图像视图未启用用户交互,这意味着它们的子视图都不能接收用户交互。
buttonPanelView.userInteractionEnabled = YES;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.