向每个单元格添加带有cornerRadius的UILabel时,UITableView会爬行

yes*_*bot 8 cornerradius uitableview uilabel ios

我正在为表视图中的每个单元格添加一个UILabel.这最初没有问题.当我使用layer.cornerRadius滚动来围绕UILabel的角落时,表格视图停止.

 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
 label1.backgroundColor = ([[managedObject valueForKey:@"color"] hasPrefix:@"FFFFFF"]) ? [UIColor blackColor] : color;
 label1.layer.cornerRadius = 10.0f;
 [cell addSubview:label1];
Run Code Online (Sandbox Code Playgroud)

Chr*_*phK 15

不需要带有角落的图像 - 请参阅fknrdcls对此问题的回答:

UILabel层cornerRadius对性能产生负面影响

基本上,您只需要为标签指定透明背景,然后将背景颜色添加到图层中.然后,您可以禁用maskToBounds,这可以大大提高性能.所以你的代码变成:

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
label1.backgroundColor = [UIColor clearColor];
label1.layer.backgroundColor=[UIColor whiteColor].CGColor;
label1.layer.cornerRadius = 10.0f;
label1.layer.masksToBounds = NO;
label1.layer.shouldRasterize = YES;
Run Code Online (Sandbox Code Playgroud)


nev*_*ing 7

我在自己之前尝试了这一点,并发现它对任何移动的视图都没用.您应该创建一个带圆角的图像,并将其添加为标签的背景.