Iphone can';t单击ui按钮时';已将添加到UIImageView

Iphone can';t单击ui按钮时';已将添加到UIImageView,iphone,uiimageview,uibutton,Iphone,Uiimageview,Uibutton,伙计们,我想在添加到UIImageVIew后单击我的uiButton,但它不起作用。 这是我的代码: UIButton *btnDetail = [[UIButton buttonWithType:UIButtonTypeDetailDisclosure]retain]; btnDetail.frame = CGRectMake(0,0,100,100); [btnDetail addTarget:self action:@selector(buttonClicked) forControlE

伙计们,我想在添加到UIImageVIew后单击我的uiButton,但它不起作用。 这是我的代码:

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

btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];

[self.imageView addSubview:btnDetail];
当我将该按钮添加到UIImageVIew时,该按钮无法单击,但如果我不将其添加到UIImageVIew,它将正常工作。
请有人帮帮我

注意:默认情况下,
UIImageView
UserInteraction
属性设置为NO。这是我们大多数人犯错误的地方。 因此,在向
UIImageView
添加任何控件时,要签入的主要内容是将其
UserInteractionEnabled
属性设置为YES

[self.imageView setUserInteractionEnabled:YES];
因此,请按如下方式修改您的代码:

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

btnDetail.frame = CGRectMake(0,0,100,100);
[btnDetail addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventAllTouchEvents];

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

HAppy Coding…

UIImageView的
userInteractionProperty
默认设置为“否”,因此它不会处理触摸,也不会将触摸传播到其子视图。尝试将其设置为“是”:

self.imageView.userInteractionEnabled = YES;

嘿,我用过这个密码。它工作正常

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 100.0, 20.0);
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"ShowView" forState:UIControlStateNormal];

[image2 addSubview:button];
[image2 bringSubviewToFront:button];
[image2 setUserInteractionEnabled:YES];

如果有人与UIImageView中的UIButton存在交互问题,请执行下一步

1创建UIImageView的属性:

@property (nonatomic, strong) UIImageView *buttonImageView;
2.然后将其合成:

@synthesize buttonImageView = _buttonImageView;
3.创建实施方法:

- (UIImageView *) buttonImageView {

    _buttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
    [_buttonImageView setBackgroundColor:[UIColor whiteColor]];
    [_buttonImageView setUserInteractionEnabled:YES];

    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [addButton setTitle:@"add button" forState:UIControlStateNormal];
    addButton.frame = CGRectMake(0, 0, 160, 50);
    [addButton setUserInteractionEnabled:YES];
    [addButton setEnabled:YES];
    [addButton setAlpha:1];
    [addButton addTarget:self action:@selector(addNewImage) forControlEvents:UIControlEventTouchUpInside];

    return _buttonImageView;
}
4例如,在viewDidLoad方法中将子视图添加到视图中:

[self.view addSubview:self.buttonImageView];

我已经尝试了你的代码,但什么也没发生,我仍然不能点击它。更多信息:在我添加UIButton之前,我已经将另一个UIImage添加到此UiVIewImage,是否有影响。??请查看新代码。。我已经实现并测试了它。现在工作正常了。。实际上,默认情况下imageview的用户交互是关闭的,因此您无法单击添加到其中的任何内容。因此,我已将其usre交互设置为已启用。现在使用finethnx,伙计们,它工作得非常完美,我简直不敢相信我的错误只是放了[self.imageView setUserInteractionEnabled:YES];在我添加子视图之前,请确保它不起作用,,,哈哈,可笑,,,顺便说一句,非常感谢大家…这件事耗费了我4-5天的时间,被这样的问题困住了。InteractionEnabled=否。帮助我可以点击UIImageView。@Imam Arief W。如果这不起作用,那么其他问题就是你的问题。