UIButton not calling event in imageView

微笑、不失礼 提交于 2019-12-13 22:58:01

问题


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

回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/19673750/uibutton-not-calling-event-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!