UIButton inside UIImageView does not respond to taps
UIImageView
has userInteractionEnabled
set to NO
/ false
by default.
You are adding the button as a subview to the image view. You should set it to YES
/ true
.
May I ask why are you adding invisible UIButtons
to UIImageView
?
Seems like a bad practice, notice Interface Builder
doesn't allow you to add UIButton
inside them.
If you want an image with touch handling, you can:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];[button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents];[button setTitle:@"point" forState:UIControlStateNormal];[button setImage:[UIImage imageNamed:@"img.jpg"] forState:UIControlStateNormal];[button setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];[scrollView addSubview:button];
You can have addInvisibleButtons implementation as
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];[button setBackgroundColor:[UIColor clearColor]];[button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents];[button setTitle:@"point" forState:UIControlStateNormal];button.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);self.imageView.userInteractionEnabled = YES;[self.imageView addSubview:button];
If you want to have UIButton
as completely invisible then you need to remove a line [button setTitle:@"point" forState:UIControlStateNormal];
since it use to show text on UIButton
which make it visible.
This may resolve your issue.