【问题标题】:Bug with rounded UIImageViews in a UICollectionViewCellUICollectionViewCell 中带有圆形 UIImageViews 的错误
【发布时间】:2015-09-27 14:33:09
【问题描述】:

所以今天我在尝试使用集合视图并遇到了一个非常有趣的错误。

错误是我拥有的 imageView 圆圈从来都不是完美的,除非我将它们从屏幕上滚动出来。它们的形状类似于带有圆形边缘的菱形。

在我再次上下滚动以使顶部的单元格暂时看不见之后,看不见的单元格现在是完美的圆圈。

这是我的单元类代码:

class FavouritesCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var profilePicutreImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!

    override var bounds: CGRect {
        didSet {
            contentView.frame = bounds
        }
    }

    override func layoutSubviews() {
        profilePicutreImageView.layer.borderWidth = 2.0
        profilePicutreImageView.layer.masksToBounds = false
        profilePicutreImageView.layer.cornerRadius = profilePicutreImageView.frame.width/2
        profilePicutreImageView.layer.borderColor = UIColor.blackColor().CGColor
        profilePicutreImageView.clipsToBounds = true

    }
}

关于这可能是什么的任何想法?

更新---这是正在发生的事情的图片

之前:

之后:

【问题讨论】:

  • 通常我使用在initFrame中设置图层属性并设置masksToBounds=true
  • 将 mastToBounds 属性设置为 true。
  • 恐怕将其设置为 true 没有帮助。您将如何使用 initFrame 进行设置?

标签: ios xcode swift uiimageview uicollectionviewcell


【解决方案1】:

我回复a similar question here

首先,我会打电话给layoutSubviews。之后,在将数据绑定到单元格时调用layoutIfNeeded

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
          cellForItemAtIndexPath:(NSIndexPath *)indexPath
     UICollectionViewCell*cell = ...
     // Do your stuff here to configure the cell
     // Tell the cell to redraw its contentView        
     [cell layoutIfNeeded];
}

【讨论】:

    【解决方案2】:

    我认为这与您在方法 layoutSubview 中设置 imageView 的边框有关,该方法将在不同情况下在单元格的提升周期中被多次调用。

    如果您使用 xib 或在情节提要中初始化单元格,则在 initWithFrame 中设置 imageView 的边框将无济于事。而是在awakeFromNib 中执行此操作。或者像这样以更快速的方式进行操作

    @IBOutlet var avatar: UIImageView! {
            didSet {
                avatar.clipsToBounds = true
                avatar.layer.cornerRadius = 37.5
            }
        }
    

    【讨论】:

    • 我尝试了 didSet 和 awakeFromNib,但 bug 并没有消失
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多