Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't click UIButton when it's added to UIImageView

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];

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

like image 695
Imam Arief W Avatar asked Aug 20 '10 08:08

Imam Arief W


2 Answers

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];

HAppy Coding...

like image 167
Suresh Varma Avatar answered Oct 16 '22 19:10

Suresh Varma


UIImageView has userInteractionProperty set to NO by default so it does not handle touches and does not propagate them to its subviews. Try to set it to YES:

self.imageView.userInteractionEnabled = YES;
like image 7
Vladimir Avatar answered Oct 16 '22 21:10

Vladimir