35

I have a scroll view, which has an image view with an image as a subview, and the image view has a UIButton as one of its subviews. The problem is, I am not able to click on the button. I can SEE the button, but I cannot tap on it.

Can someone tell me what is it that I am messing up? Any help is greatly appreciated! Thanks!

Below is the code:

scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];    
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.jpg"]];
scrollView.delegate = self;
self.view = scrollView;

// add invisible buttons
[self addInvisibleButtons];
[scrollView addSubview:imageView];

addInvisibleButtons has the following code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[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 addSubview:button];
sKhan
  • 9,694
  • 16
  • 55
  • 53
Ravi
  • 7,929
  • 6
  • 38
  • 48
  • 1
    You could try and see if bringSubviewToFront: works after adding the button to the image view? Or same could be applied to the image view within your scroll view. – Luke May 28 '11 at 21:14
  • Infact, I tried that and it didn't work. userInteractionEnabled did the trick. – Ravi May 28 '11 at 21:49

3 Answers3

101

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.

sKhan
  • 9,694
  • 16
  • 55
  • 53
Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
  • 1
    Cool, that worked. But, when I do this, the target/action method for the button is getting called twice (I had an NSLog inside the `buttonHandler` selector to confirm this). Why is that happening? – Ravi May 28 '11 at 21:44
  • 3
    Shouldn't `UIControlEventAllEvents` be `UIControlEventTouchUpInside`? – Deepak Danduprolu May 28 '11 at 21:49
  • Indeed yes! I made that change when I was trying to meddle with something else and forgot to undo the change. Great. Thanks! – Ravi May 28 '11 at 21:57
  • wise words.! aka `userInteractionEnabled` – Yash Bedi Jul 23 '20 at 07:36
4

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];
sKhan
  • 9,694
  • 16
  • 55
  • 53
Desdenova
  • 5,326
  • 8
  • 37
  • 45
  • Because I want to implement the HTML image-map kind of feature on the iPad, where clicking on several regions of the image does different things. – Ravi May 28 '11 at 21:45
  • I already tried that too, and the problem is when you zoom in and out of the map, the button location gets misplaced and you don't get the tap action where it is supposed to. – Ravi May 28 '11 at 21:56
2

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.

sKhan
  • 9,694
  • 16
  • 55
  • 53