can#39;t click UIButton when it#39;s added to UIImageView(添加到 UIImageView 时无法单击 UIButton)
问题描述
伙计们,我想在添加到 UIImageVIew 后单击我的 uiButton,但它不起作用.这是我的代码:
guys, i want to click my uiButton after it's added to UIImageVIew, but it doesn't work. this is my code :
UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];
[self.imageView addSubview:btnDetail];
当我将按钮添加到 UIImageVIew 时无法单击,但如果我不将其添加到 UIImageView,它可以正常工作.请有人帮助我
the button can't click when i add it to UIImageVIew, but if i don't add it to UIImageView, it works properly. please somebody help me
推荐答案
注意: 默认情况下 UIImageView
的 UserInteraction
属性设置为否.这是我们大多数人犯错的地方.因此,在向 UIImageView
添加任何控件时要检查的主要内容是将其 UserInteractionEnabled
属性设置为 YES.
Note: By default the UserInteraction
property of UIImageView
is set to NO. This the place where most of us makes mistake.
So the main thing to check in while adding any control to UIImageView
is to set its UserInteractionEnabled
property to YES.
[self.imageView setUserInteractionEnabled:YES];
所以修改你的代码如下:
So modify your code as below:
UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];
btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];
[self.imageView addSubview:btnDetail];
[self.imageView bringSubviewToFront:btnDetail];
[self.imageView setUserInteractionEnabled:YES];
快乐的编码...
这篇关于添加到 UIImageView 时无法单击 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:添加到 UIImageView 时无法单击 UIButton


- Android viewpager检测滑动超出范围 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01