美文网首页iOSiOS干货iOS
UICollectionViewCell 边框多一条线

UICollectionViewCell 边框多一条线

作者: Chase_Eleven | 来源:发表于2017-06-22 19:19 被阅读1086次

直接上图,红圈内多出来的一条诡异的黑线
并不是每个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的值都转为整型时,这个问题就被解决了


小白总结,欢迎打脸指正

相关文章

网友评论

  • nenhall:我也遇到了,也是因為frame 導致的
  • Code_zhou:你好,请教个问题。比如第一张图,你的第一行第三个cell再稍微宽一点(这时候一行放不下这三个了),就会显示两个,这个是没错的。但是你有没有发现前面的两个cell这时候居两边显示了(一个最左边,一个最右边,把中间空出来了,不会依然居左边显示了。。)我碰到这样的问题,看看你有没有这样的问题,指点一下。
    Code_zhou:@ac_eleven 谢谢
    Chase_Eleven:继承继承UICollectionViewFlowLayout,自己写一个类
    就可以让每个cell的间距相同
  • 96e4766b589a:这种时候不要怀疑,立马真机试一下
  • 蚂蚁牙齿不黑:这可能是模拟器的bug吧:cold_sweat:

本文标题:UICollectionViewCell 边框多一条线

本文链接:https://www.haomeiwen.com/subject/bpnfcxtx.html