iphone - 如何在 UIImageView 上制作可点击的 UIButton?

标签 iphone ios ipad uiimageview uibutton

我想创建一个 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/

相关文章:

iOS:如何检查 UIViewControllers 是否正在卸载? ( swift )

iphone - iPad是否有加速度计或陀螺仪?

iphone - 如何在 Ipad 中显示 Split View Controller 的详细 View ?

iphone - UIWebView 中的字体显示不正确

iphone - OBJC_CLASS_$_MTSCRA”,引用自

iphone - 如何确定和解释 CGImage 的像素格式

ios - SpriteKit 接头

iphone - 通过类中的方法创建标签

ios - 在 Swift 3 中将数据保存到 .plist 文件

ios - 在 Swift 中使用 NSURLSession 下载文件