将UIButton添加到UIImageView时无法单击UIButton

时间:2022-06-07 00:19:24

guys, i want to click my uiButton after it's added to UIImageVIew, but it doesn't work. this is my code :

伙计们,我想在添加到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];

the button can't click when i add it to UIImageVIew, but if i don't add it to UIImageView, it works properly. please somebody help me

当我将它添加到UIImageVIew时按钮无法单击,但如果我不将它添加到UIImageView,它可以正常工作。请有人帮帮我

4 个解决方案

#1


48  

Note: By default the UserInteraction property of UIImageView is set to NO. This the place where most of us makes mistake. So the main thing to check in while adding any control to UIImageView is to set its UserInteractionEnabled property to YES.

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

[self.imageView setUserInteractionEnabled:YES];

So modify your code as below:

所以修改你的代码如下:

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...

快乐编码......

#2


5  

UIImageView has userInteractionProperty set to NO by default so it does not handle touches and does not propagate them to its subviews. Try to set it to YES:

默认情况下,UIImageView将userInteractionProperty设置为NO,因此它不处理触摸并且不会将它们传播到其子视图。尝试将其设置为YES:

self.imageView.userInteractionEnabled = YES;

#3


3  

hey i had use this code. its working properly

嘿,我使用过这段代码。它的工作正常

 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];

#4


0  

If someone has interaction problem with UIButton in UIImageView do next steps.

如果有人在UIImageView中与UIButton有交互问题,请执行后续步骤。

1 Create property of UIImageView:

1创建UIImageView的属性:

@property (nonatomic, strong) UIImageView *buttonImageView;

2 Than synthesize it:

2比合成它:

@synthesize buttonImageView = _buttonImageView;

3 Create implementation method:

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 For example in viewDidLoad method add subview to your view:

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

[self.view addSubview:self.buttonImageView];

#1


48  

Note: By default the UserInteraction property of UIImageView is set to NO. This the place where most of us makes mistake. So the main thing to check in while adding any control to UIImageView is to set its UserInteractionEnabled property to YES.

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

[self.imageView setUserInteractionEnabled:YES];

So modify your code as below:

所以修改你的代码如下:

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...

快乐编码......

#2


5  

UIImageView has userInteractionProperty set to NO by default so it does not handle touches and does not propagate them to its subviews. Try to set it to YES:

默认情况下,UIImageView将userInteractionProperty设置为NO,因此它不处理触摸并且不会将它们传播到其子视图。尝试将其设置为YES:

self.imageView.userInteractionEnabled = YES;

#3


3  

hey i had use this code. its working properly

嘿,我使用过这段代码。它的工作正常

 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];

#4


0  

If someone has interaction problem with UIButton in UIImageView do next steps.

如果有人在UIImageView中与UIButton有交互问题,请执行后续步骤。

1 Create property of UIImageView:

1创建UIImageView的属性:

@property (nonatomic, strong) UIImageView *buttonImageView;

2 Than synthesize it:

2比合成它:

@synthesize buttonImageView = _buttonImageView;

3 Create implementation method:

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 For example in viewDidLoad method add subview to your view:

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

[self.view addSubview:self.buttonImageView];