Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILabel not showing subview in iOS8

Currently I made a back Label for background and add UIButton on UILabel. It's showing in iOS 7 but in iOS 8 it's not showing . If I replace UILabel with UIView then It's working fine.

If I use UILabel in iOS 8 using this code For showing UILabel and subview UIButton

 UILabel *downLabel = [[UILabel alloc]init];
downLabel.frame    = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );  
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f)  alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];

UIButton *downbtnobj  = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.68,downLabel.frame.size.height/10,SCREEN_WIDTH*0.30,downLabel.frame.size.height/1.25)];

UIImage *btndownImage = [UIImage imageNamed:@"img 3.png"];
[downbtnobj setImage:btndownImage forState:UIControlStateNormal];
[downbtnobj addTarget:self action:@selector(bulletInButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[downLabel addSubview:downbtnobj];

enter image description here

Now You can see only UILabel is showing but button is not showing on UILabel.

One another this If I replace UILabel by UIView then It's showing perfect.

    UIView *downLabel = [[UIView alloc]init];
downLabel.frame    = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f)  alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];

 UIButton *downbtnobj  = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*0.68,downLabel.frame.size.height/10,SCREEN_WIDTH*0.30,downLabel.frame.size.height/1.25)];
  UIImage *btndownImage = [UIImage imageNamed:@"img 3.png"];
[downbtnobj setImage:btndownImage forState:UIControlStateNormal];
[downbtnobj addTarget:self action:@selector(bulletInButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[downLabel addSubview:downbtnobj];

In this code you can see I replace only UILabel by UIView. Now button is showing. enter image description here

Can any help why subviews not showing on UILabel in iOS8

like image 276
Jogendra.Com Avatar asked Nov 29 '14 05:11

Jogendra.Com


1 Answers

It's not clear what is different about labels in iOS 8, but if you call layoutIfNeeded on your label, that fixes the problem (it needs to be after you set the frame),

UILabel *downLabel = [[UILabel alloc]init];
downLabel.frame    = CGRectMake(0, SCREEN_HEIGHT*0.92, SCREEN_WIDTH,SCREEN_HEIGHT*0.08 );  
[downLabel layoutIfNeeded];
[downLabel setBackgroundColor:[UIColor colorWithRed:(66/255.f) green:(67/255.f) blue:(63/255.f)  alpha:1]];
downLabel.userInteractionEnabled = YES;
[self.view addSubview:downLabel];

After Edit:

I also found out that if you set the text (any time after you create the label), then the button appears.

like image 134
rdelmar Avatar answered Nov 15 '22 10:11

rdelmar