46

我正在尝试将子视图添加到 UIButton。这现在工作正常。但是,一旦我添加了子视图,该按钮就不再可点击了。

我使用以下代码:

UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button addSubview:asyncImage];
[button addSubview:price];
    [button addTarget:self 
               action:@selector(buttonClicked:) 
     forControlEvents:UIControlEventTouchUpInside];

如果我剪掉 2 个 addsubviews,该按钮将再次起作用。如果有人知道如何解决这个问题,那就太好了!

太好了!!!

4

6 回答 6

82

我找到了一个快速的解决方案。我需要将 asyncimageview 设置为以下内容:

asyncImage.userInteractionEnabled = NO;
        asyncImage.exclusiveTouch = NO;

在这之后它起作用了!

于 2011-07-13T09:13:28.287 回答
3

尝试:

[UIButton buttonWithType:UIButtonTypeCustom];

代替:

[UIButton buttonWithType:UIButtonControlType];

于 2011-07-13T07:02:22.780 回答
2

这里重要的是确保userInteractionEnabled将其设置为NO. 幸运的是,它立即适用于UIImageViewand UILabel(可能适用于 a 的其他子类,UIView但这些是添加到按钮的最流行的子视图),因为默认情况下,对于此类,它NO默认设置为。不幸的是,它已设置为YESin,UIView因此请确保在这种情况下进行更改。没有必要与任何其他标志混在一起。问题的本质是很多人不知道这个标志的默认值在子类中是不同的。

于 2015-02-12T14:54:36.920 回答
1

你有没有试过把:

[asyncImage setUserInteractionEnabled:YES];
于 2011-07-13T07:03:02.350 回答
1

在相同的情况下,我执行此操作:从 UIButton 继承并将按钮的所有标签和图像视图添加到 self,最后将新按钮作为最后一个子视图查看并将 self 按钮的目标添加到最后一个按钮(还将 backgroundColor 设置为 clearColor 以实现透明)。现在它将是可点击的并且工作正常。

于 2011-07-13T07:20:17.380 回答
0

在 Swift 中,测试你的 UIButton 是否被阻塞

uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false
于 2015-07-08T00:58:45.143 回答