ios 将子视图添加到 UIButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6675233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
addSubView to UIButton
提问by Jos
I'm trying to add subviews to a UIButton. This is working fine right now. But the button isn't clickable anymore as soon as I add the subviews.
我正在尝试向 UIButton 添加子视图。这现在工作正常。但是一旦我添加了子视图,该按钮就不再可点击了。
I use the following code:
我使用以下代码:
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];
The button works again if I cut out the 2 addsubviews. If anyone knows how to fix this it would be great!
如果我删除了 2 个 addsubviews,该按钮将再次起作用。如果有人知道如何解决这个问题,那就太好了!
THNX!!!
THNX!!!
回答by Jos
I found a quick solutions. I needed to set the asyncimageview to the following:
我找到了一个快速的解决方案。我需要将 asyncimageview 设置为以下内容:
asyncImage.userInteractionEnabled = NO;
asyncImage.exclusiveTouch = NO;
After this it worked!
在这之后它起作用了!
回答by user842059
try:
尝试:
[UIButton buttonWithType:UIButtonTypeCustom];
[UIButton buttonWithType:UIButtonTypeCustom];
instead of:
代替:
[UIButton buttonWithType:UIButtonControlType];
[UIButton buttonWithType:UIButtonControlType];
回答by Matteo Alessani
Have you tried to put:
你有没有试过把:
[asyncImage setUserInteractionEnabled:YES];
回答by Valerii Pavlov
in the same sitiation i make this action: inherit from UIButton and add all labels and imageview's of button to self, finally put new button to view as last subview and add targets of self button to this last button(also set backgroundColor to clearColor for transparent). now it will be clickable and works fine.
在同一情况下,我执行此操作:从 UIButton 继承并将按钮的所有标签和图像视图添加到 self,最后将新按钮作为最后一个子视图查看,并将 self 按钮的目标添加到最后一个按钮(还将 backgroundColor 设置为 clearColor 为透明)。现在它将是可点击的并且工作正常。
回答by Julian Król
The important thing here is to make sure that userInteractionEnabled
will be set to NO
. Fortunately it works immediately for UIImageView
and UILabel
(maybe for other subclasses of a UIView
but those are the most popular subviews added to button) because by default for this classes it is set to NO
by default. Unfortunately it is set to YES
in UIView
so make sure to change it in that case. Messing around with any other flags shouldn't be necessary. The nature of problem is that many people do not know that default value of this flag is different in subclasses.
这里重要的是确保userInteractionEnabled
将设置为NO
. 幸运的是,它立即适用于UIImageView
和UILabel
(可能适用于 a 的其他子类,UIView
但这些是添加到按钮的最流行的子视图),因为默认情况下,对于此类,它被设置为NO
默认值。不幸的是它被设置为YES
inUIView
所以一定要在这种情况下改变它。没有必要与任何其他标志混在一起。问题的本质是很多人不知道这个标志的默认值在子类中是不同的。
回答by YannSteph
In Swift, test that is you have your UIButton blocked
在 Swift 中,测试您是否阻止了 UIButton
uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false