addSubView to UIButton

32,603

Solution 1

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

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

After this it worked!

Solution 2

try:

[UIButton buttonWithType:UIButtonTypeCustom];

instead of:

[UIButton buttonWithType:UIButtonControlType];

Solution 3

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.

Solution 4

Have you tried to put:

[asyncImage setUserInteractionEnabled:YES];

Solution 5

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.

Share:
32,603

Related videos on Youtube

Jos
Author by

Jos

Updated on July 08, 2020

Comments

  • Jos
    Jos almost 4 years

    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 cut out the 2 addsubviews. If anyone knows how to fix this it would be great!

    THNX!!!

  • Jos
    Jos almost 13 years
    That doesn't work, it gives me an error, doesn't recognize UIButtonControlType
  • Matteo Alessani
    Matteo Alessani almost 13 years
    have you tried also with the other view? [price setUserInteractionEnabled:YES];
  • user842059
    user842059 almost 13 years
    oh, then try setting user interaction to enabled, like this [YOUR BUTTON setUserInteractionEnabled://bool, for you its YES
  • Jos
    Jos almost 13 years
    I tried that for the 2 subviews (price and asyncImage) and for the button. Bit didn't made any difference...
  • Jos
    Jos almost 13 years
    I get somehow the idea, but don't exactly know what I need to do. Do you have a short example? Thnx!
  • Jos
    Jos almost 13 years
    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
    Jos almost 13 years
    Wow, OK! Gonna try that! Thnx
  • Jos
    Jos almost 13 years
    Will answer my questions in 2 days when possible
  • Shvalb
    Shvalb over 10 years
    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!
  • Julian
    Julian about 9 years
    @benhi, cool. If you want me to help - more details will be helpful :P
  • benhi
    benhi about 9 years
    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];
  • Julian
    Julian about 9 years
    so it doesn't detect the touches or what doesn't work?
  • benhi
    benhi about 9 years
    the label doesn't appear
  • Julian
    Julian about 9 years
    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
  • benhi
    benhi about 9 years
    the button is already initialised and I see it in my device, so the the width and height isn't 0...
  • benhi
    benhi about 9 years
    I Create the button in the storyboard
  • Julian
    Julian about 9 years
    You said that label is bot appearing and now it is so what is the truth :-P?
  • Nicholas Allio
    Nicholas Allio almost 5 years
    Simply setting userInteractionEnabled = false did the trick for me.