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