gpt4 book ai didi

ios - CollectionView Cell add 1px boarder 变为 2px

转载 作者:行者123 更新时间:2023-11-29 12:04:45 32 4
gpt4 key购买 nike

设计师需要我的每个collectionView Cell都有一个1px的边框,如下所示:

enter image description here

而且我的实现显示,好像有些边框重叠变成了2px,有没有其他简单的方法可以搞定?感谢帮助。

enter image description here

主要代码如下:

- (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;
}

最佳答案

CollectionView单元格边框重叠的解决方法:

通过CALayer()添加底部和右侧边框

objective-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];

swift :

     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)

关于ios - CollectionView Cell add 1px boarder 变为 2px,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35447389/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com