繁体   English   中英

UILabel没有在iOS8中显示子视图

[英]UILabel not showing subview in iOS8

目前我为背景制作了一个标签,并在UILabel上添加了UIButton。 它在iOS 7中显示,但在iOS 8中它没有显示。 如果我用UIView替换UILabel那么它工作正常。

如果我使用此代码在iOS 8中使用UILabel用于显示UILabel和子视图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];

在此输入图像描述

现在您只能看到UILabel正在显示,但UILabel上没有显示按钮。

如果我用UIView取代UILabel那么它就显得很完美了。

    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];

在这段代码中你可以看到我只用UIView替换UILabel。 现在按钮显示。 在此输入图像描述

可以帮助为什么子视图没有在iOS8中的UILabel上显示

目前还不清楚iOS 8中的标签有什么不同,但如果你在标签上调用layoutIfNeeded,则可以解决问题(需要在设置框架后),

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];

编辑后:

我还发现,如果您设置文本(在创建标签后的任何时间),则会出现按钮。

非常简单的方法你需要在你的UILabel上设置clipsToBounds

downLabel.clipsToBounds = YES;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM