简体   繁体   English

addTarget不适用于iOS中的UIButtons,视图被遮盖

[英]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.

相关问题 UIButton addTarget不适用于iOS7,但可在IOS6中使用 - UIButton addTarget doesn't work with iOS7,but works in IOS6 TableViewCell 中 UIButton 的 addTarget 不起作用 - addTarget for UIButton in TableViewCell doesn't work 如果按钮在自定义视图中使用 let 和 addTarget 初始化,则 UIButton 的操作不起作用,但它在 UIViewController 中起作用 - Action of UIButton doesn't work if the button is initialized with let and the addTarget in it in custom view, but it works in UIViewController 模态视图控制器覆盖方向后,iOS 6视图控制器布局不会更新 - iOS 6 view controller layout doesn't update after orientation change when covered by a modal view controller 为什么我的 addTarget 在按钮上的功能不起作用? - Why doesn't my addTarget's function on button work? UIControlStateNormal的Addtarget不起作用 - Addtarget for UIControlStateNormal doesn't works iOS UICollection视图布局不起作用 - iOS UICollection View layout doesn't work 当键盘覆盖时 TextField 向上移动不会留在 position - TextField moved up when covered by keyboard doesn't stay in position iOS键盘存在时视图不会向上移动 - View doesn't move up when keyboard is present iOS 为什么addTarget在iOS的子视图中不起作用? - Why addTarget isn't working in subview on iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM