添加到 UIImageView 时无法单击 UIButton

can#39;t click UIButton when it#39;s added to UIImageView(添加到 UIImageView 时无法单击 UIButton)

本文介绍了添加到 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

推荐答案

注意: 默认情况下 UIImageViewUserInteraction 属性设置为.这是我们大多数人犯错的地方.因此,在向 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

基础教程推荐