ios - Collectionview 网格在网格的每个角落都有圆角

标签 ios objective-c uicollectionview rounded-corners

我正在尝试实现一个简单的由灰线分隔的小矩形单元格组成的网格。此外,我的收藏 View 的四个角应该有圆角

我发现我不能只在 UICollectionView 层上设置 cornerRadius,因为那样会整个 collectionview 在一个“盒子”中,它在滚动时看起来很奇怪等等。看起来我必须在每个角处绕过 4 个单元格的角。

我遇到了挑战

  1. 同时在 4 个角单元格上设置圆角
  2. 设置单元格之间的实际边框

我将不胜感激。我设法做到了,但使用图形作为顶部、底部、左侧和右侧的边框,并模拟了圆形边缘。这对我有用,因为我的网格具有固定值,因此我可以计算并计算出每个单元格在网格中的位置。

谢谢!

最佳答案

你可以通过类似的方式设置单元格的边框

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *cell = [...]

    if (indexPath.row == 0 && indexPath.section == 0) { // top left

        [self roundView:cell.view onCorner:UIRectCornerTopLeft radius:yourRadius];

    } else [...]

}

我建议你使用类似于下一个的函数来圆化单元格的角

- (void)roundView:(UIView *)view onCorner:(UIRectCorner)rectCorner radius:(float)radius {
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer new];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    [view.layer setMask:maskLayer];
}

要设置单元格之间的边框,您应该创建自定义 UICollectionViewFlowLayout 并使用方法设置所有单元格的框架

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

关于ios - Collectionview 网格在网格的每个角落都有圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33479220/

相关文章:

ios - UICollectionView 水平分页,页面之间有空间

php - 从本地主机 :8888 in objective c using json 获取数据时出现问题

ios - 如何实现 UICollectionView 中页面之间的间距?

ios - 可扩展的 UICollectionViewCell

swift - 发件人 : UIImageView ! == CollectionViewCell 中的 UIImageView

ios - 如何快速检查类型并将其转换为 Decodable

ios - 如何用百分比颜色填充 UITableViewCell

ios - iPhone NSDateFormatter转换字符串

objective-c - 当初始值设定项不是编译时常量时,在 Swift 中使用 C 宏

iphone - 在UITableViewView中异步下载多个图像?