我想创建一个 UIButton 来显示在 UIImageView 上。我的代码片段如下所示:
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"ClickMe" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:16]];
btn.frame = CGRectMake(340, 550, 70, 50);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
[imageView addSubview:btn];
[self addSubview:imageView];
我的应用程序在我想要显示的部分图像上显示按钮,但它不响应单击图像。相反,当我在其检测时记录输出时,它确实检测到单击。但我希望这个按钮在选择时被点击并执行一些功能。
提前致谢。
瓦希布
最佳答案
我试过了,它对我有用......
UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:@"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];
UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image.jpeg"] forState:UIControlStateHighlighted];
[ButtonOnImageView setImage:[UIImage imageNamed:@"image2.jpeg"] forState:UIControlStateNormal];
[ButtonOnImageView addTarget:self action:@selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:ButtonOnImageView];
[self.view addSubview:imageView];
关于iphone - 如何在 UIImageView 上制作可点击的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7916538/