设计师需要我的每个collectionView Cell都有1px寄存器,如下所示:
我的实现显示,似乎有些边框重叠并变成2px,还有其他简单的方法来完成它吗?感谢帮助。
这是主要代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ServiceCategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ServiceCategoryCollectionViewCell" forIndexPath:indexPath];
cell.entity = _categoryList[indexPath.row];
[cell.contentView addBorderWithWidth:0.5f hexColorString:@"#EFEFF4" cornerRadius:0];
return cell;
}
- (void)setupCollectionView{
UICollectionViewFlowLayout *flowFayout = [[UICollectionViewFlowLayout alloc] init];
flowFayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowFayout.minimumInteritemSpacing = 0;
flowFayout.minimumLineSpacing = 0;
flowFayout.itemSize = CGSizeMake(self.contentView.keyWindowWidth/4, self.contentView.height);
_categoryCollectionVIew.delegate = self;
_categoryCollectionVIew.dataSource = self;
_categoryCollectionVIew.scrollEnabled = NO;
_categoryCollectionVIew.collectionViewLayout = flowFayout;
[_categoryCollectionVIew registerNib:[UINib nibWithNibName:@"ServiceCategoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ServiceCategoryCollectionViewCell"];
}
-(void)addBorderWithWidth:(float)width hexColorString:(NSString *)color cornerRadius:(CGFloat)cornerRadius{
self.layer.borderWidth = width;
self.layer.borderColor = [UIColor colorWithHexString:color alpha:1].CGColor;
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = YES;
}
答案 0 :(得分:1)
重叠CollectionView单元格边框的解决方案:
通过CALayer()
添加底部和右侧边框目标c:
CALayer *bottomBorder = [CALayer layer];
bottomBorder.borderColor = [UIColor darkGrayColor].CGColor;
bottomBorder.frame = CGRectMake(0, cell.contentView.frame.height - 1,
cell.contentView.frame.width, 1);
[cell.layer.layer addSublayer:bottomBorder];
CALayer *rightBorder = [CALayer layer];
rightBorder.borderColor = [UIColor darkGrayColor].CGColor;
rightBorder.frame = CGRectMake(cell.contentView.frame.width, 0, 1,
cell.contentView.frame.height);
[cell.layer.layer addSublayer:rightBorder];
斯威夫特:
let rightborder = CALayer()
rightborder.frame = CGRect(x:cell.frame.width - 1,y: 0, width: 1, height: cell.frame.height)
rightborder.backgroundColor = UIColor.darkGray.cgColor;
cell.layer.addSublayer(rightborder)
let bottomborder = CALayer()
bottomborder.frame = CGRect(x: 0, y: cell.frame.height - 1, width: cell.frame.width, height: 1)
bottomborder.backgroundColor = UIColor.darkGray.cgColor;
cell.layer.addSublayer(bottomborder)
答案 1 :(得分:0)
我不知道它的工作,但首先尝试全屏打开
simulator
。
答案 2 :(得分:0)
这是模拟器问题,检查设备中的代码然后进行比较。