lev*_*4ka 8 objective-c uiimageview ios sdwebimage uicollectionview
我有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;
Run Code Online (Sandbox Code Playgroud)
对于设置图像,我使用了libs SDWebImage
而且我确定,cornerRadius值是正确的(它是图像宽度的一半).此外,我没有在故事板属性中进行任何手动更改(位置设置为自动布局).我错过了什么?
您的问题是,单元格可以在之后重新布局,cellForItemAtIndexPath:并且单元格的大小将在其后更改.
为您解决方案:1)把cell.photo.layer.cornerRadius = cell.photo.bounds.size.width / 2;成
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
2)为你的UICollectionCell和覆盖layoutSubviews函数创建自定义类:
- (void)layoutSubviews {
[super layoutSubviews];
self.photo.layer.cornerRadius = self.frame.size.width / 2.0;
}
Run Code Online (Sandbox Code Playgroud)