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
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...
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With