6

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

Jogendra.Com
  • 6,394
  • 2
  • 28
  • 35
  • Are you doing this in a lifecycle method (viewDidLoad, viewWillAppear, etc.)? – kpsharp Nov 29 '14 at 06:09
  • How about if you put the button _in front_ of the label but _not_ as its subview? Does that work? If so, you've got yourself a workaround right there. – matt Nov 29 '14 at 06:11
  • I am doing this work in viewDidLoad .. – Jogendra.Com Nov 29 '14 at 06:11
  • But It's working fine in iOS 7 .. but not showing button in iOS 8 – Jogendra.Com Nov 29 '14 at 06:12
  • I think @kpsharp is suggesting that `viewDidLoad` might be too soon. Try, as an experiment, moving the code to `viewDidAppear` and see if it makes any difference... – matt Nov 29 '14 at 06:13
  • So, I can't confirm this, but I've noticed that in iOS 8, sometimes UI changes in the lifecycle methods don't always work. I think they're no longer guaranteed to run on the main thread (thus don't work). Try doing a dispatch to the main queue. – kpsharp Nov 29 '14 at 06:13
  • Another idea along @kpsharp lines might be: add the label, then do a short delay, then add the button. This gives the label a chance to finish layout before you add the button. Label layout definitely has changed in iOS 8 but I can't quite specify how. – matt Nov 29 '14 at 06:15
  • @matt It's possible viewDidLoad is "too soon," but I don't think that's the problem. Additionally, using it in viewDidAppear will cause it to be re-added whenever the screen is shown, so if you can add screens on the stack and navigate back, you'll be adding lots of labels and buttons. Additionally, a delay shouldn't be necessary either, but if it is a problem of the UI not working, dispatching all of it to the main queue will cause it to execute sequentially and prevent any problems like that from occurring - plus it's a cleaner and more understandable solution. – kpsharp Nov 29 '14 at 06:19
  • I changed the label's backgroundColor to clearColor then this problem soloved. – 无夜之星辰 Jan 05 '17 at 09:52

2 Answers2

8

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Nice. There is definitely something new in iOS 8 about when labels get layout and what effect that has on the interface as a whole. I had a lot of trouble with this in early betas of iOS 8 especially. – matt Nov 29 '14 at 06:32
  • Still works in iOS 10. Worth noting that setting `text` to `nil` can cause the subview to disappear again, so keeping the text non-nil (even to `""` if appropriate) is probably the best solution. – CupawnTae Sep 09 '17 at 21:54
  • thk, it fixed my problem. – Mistrx丶 Jan 08 '18 at 04:05
1

Very Simple way you need set clipsToBounds on your UILabel

downLabel.clipsToBounds = YES;

Jugal K Balara
  • 917
  • 5
  • 15