iOSiOS干货iOS

UICollectionViewCell 边框多一条线

2017-06-22  本文已影响1086人  Chase_Eleven

直接上图,红圈内多出来的一条诡异的黑线
并不是每个Cell都会有,也不是每次都会出现

诡异的黑线.png

到现在也还没有找到问题到底出在哪里
公司项目紧,搁置了好几天没去理会

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        _label = [[UILabel alloc] initWithFrame:self.bounds];
        _label.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
        _label.font = [UIFont systemFontOfSize:14];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.00];
        [self.contentView addSubview:_label];
        
    }
    return self;
}

今天抽出来点时间研究了一下

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        _label = [[UILabel alloc] initWithFrame:CGRectIntegral(self.bounds)];
        _label.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
        _label.font = [UIFont systemFontOfSize:14];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.textColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.33 alpha:1.00];
        [self.contentView addSubview:_label];
        
    }
    return self;
}
没有诡异的黑线.png

居然,解决了...

上代码

_label = [[UILabel alloc] initWithFrame:self.bounds];
_label = [[UILabel alloc] initWithFrame:CGRectIntegral(self.bounds)];

CGRectIntegral()
这个函数可以将小数类型的值转为整型

frame的数值为小数时,像素渲染到屏幕上时会产生奇怪的黑影,就是那条诡异的黑线产生的原因
CGRectIntegral() 将frame的值都转为整型时,这个问题就被解决了


小白总结,欢迎打脸指正

上一篇 下一篇

猜你喜欢

热点阅读