我如何在 UICollectionViewCell 中舍入 UIImageView?

How I can round UIImageView in UICollectionViewCell?

在我的 UICollectionViewCell class 我写了这个:

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myImageView.layer.cornerRadius  = CGRectGetHeight(self.myImageView.frame) / 2;
    self.myImageView.layer.masksToBounds = YES;
}

但这并不是在所有情况下都能正常工作,它看起来像这样:

我遇到了类似的问题trying to get a collectionView inside a TableView Cell

如果您要更改单元格的模型,您应该告诉单元格重新计算其布局,以便单元格更改其大小并调用 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];
    }