【问题标题】:UIImageView should be as circle view instead of diamond in UICollectionViewUIImageView 应该是圆形视图而不是 UICollectionView 中的菱形
【发布时间】:2015-08-10 20:27:32
【问题描述】:

我有 UICollectionView 和 UIImageView (如图所示)。

我想把它做成圆形视图。但是,当我运行应用程序时,它显示为菱形(下图)。

在(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 我将其设置为

[cell.photo setImageWithURL:[NSURL URLWithString:url] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    cell.photo.layer.cornerRadius = cell.photo.bounds.size.width / 2;
    cell.photo.layer.masksToBounds = NO;
    cell.photo.clipsToBounds = YES;

对于设置图像,我使用了 libs SDWebImage

我敢肯定,cornerRadius 的值是正确的(它是图像宽度的一半)。此外,我没有对故事板属性进行任何手动更改(位置设置为自动布局)。 我错过了什么?

【问题讨论】:

    标签: ios objective-c uiimageview uicollectionview sdwebimage


    【解决方案1】:

    你的问题是cellForItemAtIndexPath:之后单元格可以重新布局,之后单元格的大小会改变。

    为您提供的解决方案: 1) 将cell.photo.layer.cornerRadius = cell.photo.bounds.size.width / 2; 放入

    - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
    

    2) 为您的 UICollectionCell 创建自定义类并覆盖 layoutSubviews 函数:

    - (void)layoutSubviews {
        [super layoutSubviews];
        self.photo.layer.cornerRadius = self.frame.size.width / 2.0;
    }
    

    【讨论】:

    • 将 '(void)layoutSubviews' 添加到自定义 UICollectionCell 中,就像魔术一样工作。谢谢。
    • layoutSubviews 会为我做的。
    【解决方案2】:

    这对我帮助很大。

    在 UICollectionViewCell 的子类中,CustomCell.m:

    -(void)layoutSubviews
    {
        [super layoutSubviews];
    
        [self layoutCell];
    }
    
    -(void)layoutCell
    {
        [self layoutIfNeeded];
    
        [[imgThumbView layer] setCornerRadius:imgThumbView.bounds.size.width/2];
    
        [[imgThumbView layer] setBorderColor:[UIColor redColor];
    
        [[imgThumbView layer] setBorderWidth:1.5f];
    
        [[imgThumbView layer] setMasksToBounds:YES];
    }
    

    【讨论】:

      【解决方案3】:

      要使您的图像视图成为一个圆形,您必须确保您的框架是一个完美的正方形,并将角半径设置为大约 60。 这对我有用,希望对你有帮助!

      【讨论】:

      • 如果我有最大的图像怎么办?在我的情况下,我有 170x170 大小,所以半径是 85
      • 我的错误,你对我来说是正确的解决方案是在视图中 didWillAppear 添加代码 self.imageview.layer.cornerRadius = self.imageview.bounds.size.width/2; [self.imageview.layer setMasksToBounds:YES];并确保在 xib 中标记了 imageview clipsubviews 复选框。希望对您有所帮助。
      • 谢谢,是的,这适用于 UITableView,但不适用于 UICollectionView :(
      【解决方案4】:

      您的cornerRadius 值是正确的,但cell.photo.layer.masksToBounds = NO;which 错误地将其设置为cell.photo.layer.masksToBounds = YES;,如果它仍然不起作用,请确保您的ImageView 的高度和宽度应该相同。

      【讨论】:

        猜你喜欢
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        相关资源
        最近更新 更多