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
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01