UILabel does not show subview in iOS8

Currently, I have made a backtick for the background and added UIButton to UILabel. It is displayed in iOS 7, but in iOS 8 it is not displayed. If I replaced the UILabel UIView, then it will work fine.

If I use UILabel in iOS 8 using this code To display 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 that only UILabel is shown, but the button does not appear on UILabel.

One other is this. If I replaced UILabel with a UIView, then it would show 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 see that UIView replaces only UILabel. The button is now displayed. enter image description here

Can there be any help why subviews not showing up on UILabel in iOS8

+6
source share
2 answers

It is not clear what distinguishes labels in iOS 8, but if you call layoutIfNeeded on your shortcut, this fixes the problem (it should be after setting 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 editing:

I also found out that if you set the text (anytime after creating the label), a button will appear.

+8
source

A very easy way that you need to set clips for your UILabel

downLabel.clipsToBounds = YES;

+1
source

Source: https://habr.com/ru/post/978858/


All Articles