在ios开发过程中,有时候我们需要把UIButton加载到一个View上,然后在把这个View加到界面上,如果这时候你对UIButton加了点击事件,你会发现UIButton的点击事件不起作用。这时候你只需要对UIButton的父控件启用一个属性就可以了。
UIImageView *titleBgImageView = [[UIImageView alloc] initWithFrame:bgFrame];
[titleBgImageView setImage:image];
//一定要加下面这句话,否则UIButton的点击事件不起作用
titleBgImageView.userInteractionEnabled = YES;
[self.view addSubview:titleBgImageView];
_btnNanTong = [UIButton buttonWithType:UIButtonTypeCustom];
[_btnNanTong addTarget:self action:@selector(onTitleItemClick:) forControlEvents:UIControlEventTouchUpInside];
[titleBgImageView addSubview:_btnNanTong];
本文介绍了一种在iOS开发中解决UIButton点击事件不起作用的问题。通过为UIButton的父控件UIImageView启用用户交互属性,可以确保按钮响应点击事件。

1万+

被折叠的 条评论
为什么被折叠?



