我已将UILabel添加到自定义单元格的contentView中。我遇到了一个奇怪的副作用。在右边界有一个灰色边缘。我不知道为什么。我刚刚添加了一个普通的UILabel,我什么都没做。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"value1";
label.textAlignment = UITextAlignmentCenter;
[self.contentView addSubview:label];
- (void)layoutSubviews
{
UILabel *label = [self.contentView.subviews objectAtIndex:i];
label.frame = CGRectMake(…);
}
答案 0 :(得分:23)
我知道这是一个老问题,但我刚遇到同样的问题,所以我想我可以帮助别人看。
对我来说,问题是我的框架中有浮动数字。以下为我解决了这个问题:
CGRectIntegral( myLabel.frame )
答案 1 :(得分:8)
我不知道原因,但解决办法是摆脱那个灰色边缘,将单元格的背景颜色设置为clearColor。
label.backgroundColor = [UIColor clearColor];
答案 2 :(得分:4)
首先,您需要设置:
[label setBackgroundColor:[UIColor clearColor]];
接下来,设置所需的颜色:
[label layer] setBackgroundColor:[UIColor redColor].CGColor];
答案 3 :(得分:0)
在iOS 12上仍然会发生这种情况。
Ad Taylor 的Swift 版本:
myLabel.frame.integral