UIButton does not work when it in UIScrollView

2019-01-09 00:50发布

My structure of views:

UITableView
  UITableViewCell
    UIScrollView
      CustomView
        UIButton

The problem is UIButton doesn't work when I touch it. I create it with code:

btn = [[UIButton alloc] init];
[btn setImage:image forState:UIControlStateNormal];
[btn addTarget:self action:@selector(tileTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
btn.layer.zPosition = 1;
[self addSubview:btn];

I've also add this:

scroll.delaysContentTouches = NO;
scroll.canCancelContentTouches = NO;

But my button doesn't work anyway. I can't cancelContentTouches or set delay for UITableView because if I set it, UITableView stop scrolling verticaly.

Thanks for any help!

8条回答
该账号已被封号
2楼-- · 2019-01-09 01:53

I had same issue & same hierarchy of the views, With latest sdk , just use it :

Setting delaysContentTouches to NO for UIButton in the same UITableViewCell.

self.tableView.delaysContentTouches = NO
查看更多
欢心
3楼-- · 2019-01-09 01:54

Your CustomView's frame could be interfering with your UIButton. Are they overlapping? Add your button to the CustomView just to test by [CustomView addSubview:UIButton]; (using your values of course). If it works, then your issue is most likely an overlap.

查看更多
登录 后发表回答