简体   繁体   中英

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?

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..

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

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

Just do self.red.userInteractionEnabled = YES.

UIImageView has userInteractionEnabled NO as default.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM