开发者

can't click UIButton when it's added to UIImageView

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

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 he开发者_开发知识库lp me


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.

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


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:

self.imageView.userInteractionEnabled = YES;


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


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

1 Create property of UIImageView:

@property (nonatomic, strong) UIImageView *buttonImageView;

2 Than synthesize it:

@synthesize buttonImageView = _buttonImageView;

3 Create implementation method:

- (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:

[self.view addSubview:self.buttonImageView];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜