[英]How to make clickable UIButton over UIImageView?
I want to create a UIButton to display over an UIImageView . 我想创建一个UIButton来显示在UIImageView上 。 My code snippet is as shown below:
我的代码段如下所示:
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];
My application displays button exactly on part of image where i want to but it doesn't responds to click image. 我的应用程序在我想要的图像的一部分上显示按钮,但它不响应点击图像。 Instead it does detect single tap as i log output on its detection.
相反,它确实检测到单击,因为它在检测时记录输出。 But i want this button to get clicked on selection and to perform some function.
但我希望这个按钮被点击选择并执行一些功能。
Thanks in advance. 提前致谢。
wahib wahib
i tried this and it works for me....... 我试过这个,它对我有用.......
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];
you have to add define button as custom.It will work for you. 你必须添加定义按钮作为custom.It将为你工作。
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom]; UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom];
您只需使用一个按钮即可:
[UIButton setImage:[imageNamed:@"image.png"]];
you need to add the button as a subclass of a 你需要添加按钮作为a的子类
scrollview
滚动视图
not 不
imageview
ImageView的
.. it wont be clickable if the button is a subclass of a uiimageview .. assign a speacial tag for each button to recognize the clicked button ..如果按钮是uiimageview的子类,它将无法点击..为每个按钮分配一个特殊标签以识别单击的按钮
hope this helps 希望这可以帮助
try setting the userinteraction of the button enabled. 尝试设置启用按钮的用户交互。
[btn setEnabled:YES];
Coz the UIImageView will set the userinteraction of the button disabled by default. 因为UIImageView将默认设置禁用按钮的用户交互。
Else you could try this.Just change the last two lines. 否则你可以试试这个。只需改变最后两行。
Replace: 更换:
[imageView addSubview:btn];
[self addSubview:imageView];
with this: 有了这个:
[self addSubview:imageView];
[self addSubview:btn];
Make sure you add the imageview first and the button second as i have mentioned. 确保首先添加imageview,然后按照我提到的那样添加第二个按钮。 else the button would b placed behind the imageview and would seems hidden.
否则按钮会放在imageview后面,似乎是隐藏的。
If it is clickable image I prefer image over button. 如果它是可点击的图像,我更喜欢图像超过按钮。
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:@"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
I did something like this in one of my apps. 我在我的一个应用程序中做了类似的事情。 I created a xib file that contained a series of images, each with a button drawn over the top.
我创建了一个包含一系列图像的xib文件,每个图像都有一个从顶部绘制的按钮。 I connected those to the corresponding outlets in the class file.
我将它们连接到类文件中的相应插座。 I then worked out which was clicked in touchesBegan using:
然后我解决了在touchesBegan点击使用:
if(CGRectContainsPoint(btnFirstButton.frame, touchLocation)){
//do something useful
}else if(CGRectContainsPoint(btnSecondButton.frame, touchLocation)){
//do something useful
}
Hope that helps - worked for me :-) 希望有所帮助 - 为我工作:-)
I am able to show UIButton on UIImageVIew and this is the update code. 我能够在UIImageVIew上显示UIButton,这是更新代码。 I have mentioned the recent problems at bottom.
我已经提到了最近的问题。
imageView = [[UIImageView alloc] initWithImage:image]; //initWithImage:image
//imageView.tag = i; // tag our images for later use when we place them in serial form
[imageView setContentMode:UIViewContentModeScaleToFill]; //though it has no impact
imageView.userInteractionEnabled = YES; //to enable touch interaction with image
[self addSubview:imageView];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeContactAdd] retain]; //Plus icon button
btn.frame = CGRectMake(215, 550, 100, 100);
[btn addTarget:self action:@selector(onClickHotSpotButton) forControlEvents:UIControlEventTouchUpInside]; //it wasnt working
**[self addSubview:btn]; //Buttons are clickable but shows button on all images**
//[imageView addSubview:btn]; //Buttons are not clickable and are shown on all images
I have two choices rite now. 我现在有两个选择。 Whether i make my button a subview of ImageView or ScrollView which is parent of ImageView.
我是否将我的按钮设为ImageView或ScrollView的子视图,它是ImageView的父级。 If i make button subview of Scrollview like [self addSubView:btn];
如果我像[self addSubView:btn]那样制作Scrollview的按钮子视图; it displays at right position and is clickable but problem is that it is created on all images in the queue because i am making it a subview of parent view ie scrollview.
它显示在正确的位置并且是可点击的但问题是它是在队列中的所有图像上创建的,因为我将它作为父视图的子视图,即scrollview。 Else if i make it subview of child view ie ImageView which is unique for every image, but still its shown on all images and its not clickable :/
否则,如果我将其视为子视图的子视图,即ImageView,它对于每个图像都是唯一的,但仍然显示在所有图像上且不可点击:/
Can any1 guide me on how to make it clickable and keeping the linkage between dynamic button and the image unique so that i have different buttons at various positions on each image in the queue. any1可以指导我如何使其可点击并保持动态按钮和图像之间的链接唯一,以便我在队列中的每个图像上的不同位置具有不同的按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.