Iphone 当UIButton';添加到UIImageView的不工作?

Iphone 当UIButton';添加到UIImageView的不工作?,iphone,ios,ipad,Iphone,Ios,Ipad,可能重复: 我有一个应用程序,其中我添加了一个图像视图,而不是导航栏视图。像这样在视图中没有加载方法 curtainsView=[[UIImageView alloc]init]; curtainsView.image=[UIImage imageNamed:@"curtains.png"]; curtainsView.frame=CGRectMake(0,-44, 320, 60); curtainsView.backgroundColor=[UIColor clearColor]; [

可能重复:

我有一个应用程序,其中我添加了一个图像视图,而不是导航栏视图。像这样在视图中没有加载方法

curtainsView=[[UIImageView alloc]init];

curtainsView.image=[UIImage imageNamed:@"curtains.png"];
curtainsView.frame=CGRectMake(0,-44, 320, 60);
curtainsView.backgroundColor=[UIColor clearColor];

[self.view addSubview:curtainsView];
现在我想在图像视图上添加一个按钮,我正试图通过这个按钮来实现这一点

UIImage *button2Image = [UIImage imageNamed:@"scroll_down.png"];
UIButton *scroll = [UIButton buttonWithType:UIButtonTypeCustom];
//scroll = [[UIButton alloc] init];
//self.scroll.tag=100;
[scroll setImage:button2Image forState:UIControlStateNormal];
scroll.frame = CGRectMake(270,-30, 40,40);
[scroll addTarget:self action:@selector(backpressed) forControlEvents:UIControlEventTouchUpInside]; 
//[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
不工作。然后我也试过了

UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain];

btnDetail.frame = CGRectMake(260,-15,100,100);
[btnDetail addTarget:self action:@selector(backpressed) forControlEvents:UIControlEventAllTouchEvents];

[self.curtainsView addSubview:btnDetail];
[self.curtainsView bringSubviewToFront:btnDetail];
[self.curtainsView setUserInteractionEnabled:YES];

也不起作用。如果它位于图像视图的一侧,则它不工作,但当我尝试添加第二个代码时,它的工作框架是
CGRectMake(260,0100100)
。但我需要那个按钮在图像视图中。有人能帮我吗?

您不应该在ImageView上添加子视图。 而是将按钮添加到imageView上方的self.view。 并且不要将按钮添加到其superview边界之外

UIImage *button2Image = [UIImage imageNamed:@"scroll_down.png"];
UIButton *scroll = [UIButton buttonWithType:UIButtonTypeCustom];
[scroll setImage:button2Image forState:UIControlStateNormal];
scroll.frame = CGRectMake(270, 0, 40, 40);
[scroll addTarget:self action:@selector(backpressed) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:scroll];
这将有助于:

UIImageView *imgview=[[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 300, 300)];
imgview.backgroundColor=[UIColor whiteColor];
imgview.userInteractionEnabled=YES;
[self.view addSubview:imgview];


UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.Frame=CGRectMake(10, 10, 100, 20);
btn.backgroundColor=[UIColor blueColor];
[btn addTarget:self action:@selector(clickActonOfButtons) forControlEvents:UIControlEventTouchUpInside];
[imgview addSubview:btn];
btn=nil;
[imgview release];
注意:如果您要在按钮上添加图像。。。在这种情况下
imgview.userInteractionEnabled=NO

将ImgView的userInteractionEnabled设置为“是”,因为默认设置为“否”

UIImageView中的触摸未传递到UIButton

curtainsView.userInteractionEnabled=YES;
还将UIButton的userInteractionEnabled设置为YES

btnDetail.userInteractionEnabled=YES;
当设置像CGRectMake(260,-15100100)这样的帧时,还要检查btnDetail的边界是否在SuperView边界之外


当u设置CGRectMake(260,0100100)时,它工作,因为btnDetail的边界在可视区域内,并且在SuperView边界内

您需要在视图上添加一个按钮,而不是在图像视图上。 将按钮添加到self.view而不是imageview

    [self.view addSubview:btnDetail];
    [self.view bringSubviewToFront:btnDetail];

它会很好用。

我也试过了。第一个。不工作不工作,因为你的按钮超出了它的超级视图的框架。(scroll.frame=CGRectMake(270,-30,40,40);)那么我如何添加到该imageview。?如我所说,将您的按钮添加到imageview的superview上,在imageview上方,并将其设置为frame,使其位于其superview边界内。为此,也许你可以增加superview的框架。如果你用代码编辑你的答案,那就太好了。我没有正确地得到你的答案?只要检查你的btnPress事件是否正在调用或没有重复问题,因为curtainsView是UIButton的superview。默认情况下,UIImageView的userInteractionEnabled为否,它将可见。所以,在IImageView的userInteractionEnabled为YES之前,您不能触摸按钮