46

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!

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Jos
  • 2,421
  • 5
  • 26
  • 30

7 Answers7

83

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

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

After this, it worked!

Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Jos
  • 2,421
  • 5
  • 26
  • 30
  • Will answer my questions in 2 days when possible – Jos Jul 13 '11 at 09:13
  • 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! – Shvalb Nov 25 '13 at 04:43
  • 2
    Simply setting `userInteractionEnabled = false` did the trick for me. – Nicholas Allio Jul 17 '19 at 11:53
3

try:

[UIButton buttonWithType:UIButtonTypeCustom];

instead of:

[UIButton buttonWithType:UIButtonControlType];

James Zaghini
  • 3,895
  • 4
  • 45
  • 61
user842059
  • 73
  • 3
  • 12
  • That doesn't work, it gives me an error, doesn't recognize UIButtonControlType – Jos Jul 13 '11 at 07:07
  • oh, then try setting user interaction to enabled, like this [YOUR BUTTON setUserInteractionEnabled://bool, for you its YES – user842059 Jul 13 '11 at 07:15
  • I tried that for the 2 subviews (price and asyncImage) and for the button. Bit didn't made any difference... – Jos Jul 13 '11 at 07:18
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.

Julian
  • 9,299
  • 5
  • 48
  • 65
  • @benhi, cool. If you want me to help - more details will be helpful :P – Julian Mar 17 '15 at 10:45
  • 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]; – benhi Mar 17 '15 at 10:47
  • so it doesn't detect the touches or what doesn't work? – Julian Mar 17 '15 at 10:51
  • the label doesn't appear – benhi Mar 17 '15 at 10:53
  • 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 touches – Julian Mar 17 '15 at 10:58
  • the button is already initialised and I see it in my device, so the the width and height isn't 0... – benhi Mar 17 '15 at 11:01
  • I Create the button in the storyboard – benhi Mar 17 '15 at 11:01
  • You said that label is bot appearing and now it is so what is the truth :-P? – Julian Mar 17 '15 at 11:11
1

Have you tried to put:

[asyncImage setUserInteractionEnabled:YES];
Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
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.

Valerii Pavlov
  • 1,986
  • 1
  • 19
  • 30
  • I get somehow the idea, but don't exactly know what I need to do. Do you have a short example? Thnx! – Jos Jul 13 '11 at 07:29
  • 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! – Jos Jul 13 '11 at 07:52
  • Wow, OK! Gonna try that! Thnx – Jos Jul 13 '11 at 08:17
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 
zef_s
  • 11
  • 2
0

In Swift, test that is you have your UIButton blocked

uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false
YanSte
  • 10,661
  • 3
  • 57
  • 53