UIButton inside UIImageView does not respond to taps

后端 未结 3 765
走了就别回头了
走了就别回头了 2020-12-29 20:18

I have a scroll view, which has an image view with an image as a subview, and the image view has a UIButton as one of its subviews. The problem is, I am not abl

3条回答
  •  爱一瞬间的悲伤
    2020-12-29 20:38

    May I ask why are you adding invisible UIButtons to UIImageView?

    Seems like a bad practice, notice Interface Builder doesn't allow you to add UIButton inside them.

    If you want an image with touch handling, you can:

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
    [button setTitle:@"point" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"img.jpg"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
    
    [scrollView addSubview:button];
    

提交回复
热议问题