mobearette
mobearette

Reputation: 397

Click on UIButton Does Not Work if it's Added as Subview to UIImageView

I have a simple UIImageView that is animated, and i need to detect a tap on it. I've added a UIButton that is a subview of this UIImageView. However,the tap on it does not work. I've already tested the UIButton itself when added to the view,and it works, so it's not the button that is causing the problem.

Here's the code:

self.red = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 85, 100)];
self.red.image = [UIImage imageNamed:@"red.png"];

[self.red setUserInteractionEnabled: YES];
[self.view addSubview: self.red];
[self.view bringSubviewToFront:self.red]; 

UIButton *redButton = [UIButton buttonWithType: UIButtonTypeCustom];
redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height);
[redButton addTarget: self action:@selector(correctButtonClick) forControlEvents: UIControlEventAllTouchEvents];
[redButton setUserInteractionEnabled:YES];

[self.red addSubview: redButton];
[self.red bringSubviewToFront:redButton]; 

What am I doing wrong here?

Upvotes: 0

Views: 4931

Answers (7)

user1047504
user1047504

Reputation: 568

The custom button has no UI. So you can't select it. (you can try it with bound button , or add some image to button and set button alpha to 0.1 or else)

@tkanzakic answer should be correct. Just add TapGestureRecognizer to ImageView or add Image to Button.

Upvotes: 1

Mr. Frank
Mr. Frank

Reputation: 288

It's simple, the button doesn't respond because its frame is wrong and so it's not where you think it is. Basically change your line below:

redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height);

with this:

redButton.frame = CGRectMake(self.red.bounds.origin.x, self.red.bounds.origin.y, self.red.frame.size.width, self.red.frame.size.height);

Upvotes: 0

Vinayak Kini
Vinayak Kini

Reputation: 2919

Instead of using separate imageview and Button, make use of the imageview property within the Button. Which will solve all your problems and relieves you from writing extra lines of code.

UIButton *redButton = [UIButton buttonWithType: UIButtonTypeCustom];

redButton.imageView.image = [UIImage imageNamed:@"red.png"];

redButton.frame = CGRectMake(50, 300, 85, 100);
[redButton addTarget: self action:@selector(correctButtonClick) forControlEvents: UIControlEventAllTouchEvents];
[redButton setUserInteractionEnabled:YES];

[self.red addSubview: redButton];

Upvotes: 0

Firdous
Firdous

Reputation: 4652

What you have to do is to invert your implementation, adding UIButton to UIImageView is not a good idea, rather create a custom styled UIButton programatically, and add UIImageView as its subview, would definitely work

EDIT: As for you current implementation, youve added UIButton inside a UIImageView so probably you want every part of image to accept the click event, in that case your x and y coords of UIButton would be 0,0 rather than red.x and red.y, however width and height are correct

Upvotes: 2

tkanzakic
tkanzakic

Reputation: 5499

Instead of inserting a UIButton as a subview use a UITapGestureRecognizer, for example:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(correctButtonClick)];
[self.red addGestureRecognizer:tap];

Upvotes: 3

Mani
Mani

Reputation: 17585

Instead of this line , redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height); try this

redButton.frame = CGRectMake(0,0, self.red.frame.size.width, self.red.frame.size.height); 

EDIT : This line [self.red bringSubviewToFront:redButton]; is no needed..

Upvotes: 3

Avi Tsadok
Avi Tsadok

Reputation: 1843

Just do self.red.userInteractionEnabled = YES.

UIImageView has userInteractionEnabled NO as default.

Upvotes: 1

Related Questions