UIButton not calling event in imageView

2019-09-22 03:40发布

I have imageview in which I am adding UIButton it shows button when I click on button it does not call the action of the button. Here is the code I am using I have also set the imageView.userInteraction=enabled but still it is not calling the action.

UIImageView*myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(75,10,200,150)];


myImageView.image=thumbnail;

myImageView.userInteractionEnabled=YES;


[scrollView addSubview:myImageView];

UIButton*playButton=[[UIButton alloc] initWithFrame:CGRectMake(20,20,128,128)];

//[playButton setImage:[UIImage imageNamed:@"playIconPlay.png"] forState:UIControlStateNormal];

[playButton.buttonType:UIButtonTypeRoundedRect];

scrollView.userInteractionEnabled=YES;
myImageView.userInteractionEnabled=YES;
[playButton setTitle:@"Play Click" forState:UIControlStateNormal];

[playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


[playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];


myImageView.userInteractionEnabled=YES;

[myImageView addSubview:playButton];

3条回答
霸刀☆藐视天下
2楼-- · 2019-09-22 04:08

Before adding your Button as subview, please set the exclusiveTouch property to YES.

playButton.exclusiveTouch = YES
[myImageView addSubview:playButton];

Hope it Will help you.... enjoy coding..!!!

查看更多
Fickle 薄情
3楼-- · 2019-09-22 04:24

you can add a UIImageView and and UIButton in a UIView, hope it will help for you.

UIView* view = [[UIView alloc] initWithFrame:CGRectMake(75, 10, 200, 150)];

UIImageView*myImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,200,150)];


myImageView.image=thumbnail;


[view addSubview:myImageView];

[scrollView addSubview:view];

UIButton*playButton=[[UIButton alloc] initWithFrame:CGRectMake(20,20,128,128)];

//[playButton setImage:[UIImage imageNamed:@"playIconPlay.png"] forState:UIControlStateNormal];

[playButton.buttonType:UIButtonTypeRoundedRect];

[playButton setTitle:@"Play Click" forState:UIControlStateNormal];

[playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


[playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];


myImageView.userInteractionEnabled=YES;

[view addSubview:playButton];
查看更多
男人必须洒脱
4楼-- · 2019-09-22 04:27
UIButton*playButton = [UIButton buttonWithType:UIButtonTypeCustom];

Try this.. It will work Perfectly..

[playButton setImage:[UIImage imageNamed:@"playIconPlay.png"] forState:UIControlStateNormal];

[playButton setFrame:CGRectMake(20,20,128,128)];

[playButton setTitle:@"Play Click" forState:UIControlStateNormal];

[playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[playButton addTarget:self action:@selector(playButtonAction) forControlEvents:UIControlEventTouchUpInside];


[scrollView addSubview:playButton];
[scrollView bringSubviewToFront:playButton];
查看更多
登录 后发表回答