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
基础教程推荐
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
