Add subview to UIButton
Asked Answered
S

7

48

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.

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 remove the 2 addSubview: methods. If anyone knows how to fix this it would be great!

Shields answered 13/7, 2011 at 7:0 Comment(0)
S
86

I found a quick solutions. I needed to set the asyncImageView to the following:

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

After this, it worked!

Shields answered 13/7, 2011 at 9:13 Comment(3)
Will answer my questions in 2 days when possibleShields
I was thinking the opposite regarding the 'userInteractionEnabled' property, I thought the setting it to "YES" should actually catch click event on the subview of the button. GREAT ANSWER!Kiser
Simply setting userInteractionEnabled = false did the trick for me.Wuhsien
T
3

try:

[UIButton buttonWithType:UIButtonTypeCustom];

instead of:

[UIButton buttonWithType:UIButtonControlType];

Thrift answered 13/7, 2011 at 7:2 Comment(3)
That doesn't work, it gives me an error, doesn't recognize UIButtonControlTypeShields
oh, then try setting user interaction to enabled, like this [YOUR BUTTON setUserInteractionEnabled://bool, for you its YESThrift
I tried that for the 2 subviews (price and asyncImage) and for the button. Bit didn't made any difference...Shields
H
2

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.

Hymettus answered 12/2, 2015 at 14:54 Comment(8)
@benhi, cool. If you want me to help - more details will be helpful :PHymettus
Julian @property (weak, nonatomic) IBOutlet UIButton *popular; popular = [UIButton buttonWithType:UIButtonTypeCustom]; UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, popular.frame.size.width, popular.frame.size.height)]; label.text = @"ButtonTitle"; label.textColor = [UIColor redColor]; label.userInteractionEnabled = NO; [popular addSubview:label];Breakwater
so it doesn't detect the touches or what doesn't work?Hymettus
the label doesn't appearBreakwater
what frames do you give it, it might be that the width and height is 0? are you sure that at that time the button is already initialised? Btw the not showing label is a bit offtopic here ;) as the problem here was that the added subview disabled a touchesHymettus
the button is already initialised and I see it in my device, so the the width and height isn't 0...Breakwater
I Create the button in the storyboardBreakwater
You said that label is bot appearing and now it is so what is the truth :-P?Hymettus
H
1

Have you tried to put:

[asyncImage setUserInteractionEnabled:YES];
Hippolytus answered 13/7, 2011 at 7:3 Comment(1)
have you tried also with the other view? [price setUserInteractionEnabled:YES];Hippolytus
L
1

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.

Loni answered 13/7, 2011 at 7:20 Comment(3)
I get somehow the idea, but don't exactly know what I need to do. Do you have a short example? Thnx!Shields
I mainly don't understand what you mean with making the action inherit from UIButton, and setting it to self. If I do self.button it gives me an error. Thnx!Shields
Wow, OK! Gonna try that! ThnxShields
U
1

I added a label to the subview button. For a very long time I was looking for why my text in the button is not clickable. Solution in this thread:

myLable.isUserInteractionEnabled = false 
Untwine answered 21/2, 2023 at 13:3 Comment(0)
S
0

In Swift, test that is you have your UIButton blocked

uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false
Spotted answered 8/7, 2015 at 0:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.