简体   繁体   English

在UICollectionView中,UIImageView应该是圆形视图而不是菱形

[英]UIImageView should be as circle view instead of diamond in UICollectionView

I have UICollectionView with UIImageView on it (as shown at image). 我有UICollectionView和UIImageView(如图所示)。

在此输入图像描述

And I want to make it as circle view. 我想把它作为圆形视图。 But, when I running application, it's appearing as diamond (image below). 但是,当我运行应用程序时,它显示为菱形(下图)。

在此输入图像描述

In (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { I setup it as (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;

For setup image I used libs SDWebImage 对于设置图像,我使用了libs SDWebImage

And I'm sure, that cornerRadius value is correct (it's half of image width). 而且我确定,cornerRadius值是正确的 (它是图像宽度的一半)。 Also I didn't make any manual changes in storyboard properties (position set as auto layout). 此外,我没有在故事板属性中进行任何手动更改(位置设置为自动布局)。 What I missed? 我错过了什么?

Your problem is that cell can be re-layout after cellForItemAtIndexPath: and size of cell will be changed after it. 你的问题是cellForItemAtIndexPath:后可以重新布局单元格,并且单元格的大小将在它之后改变。

Solutions for you: 1) Put cell.photo.layer.cornerRadius = cell.photo.bounds.size.width / 2; 解决方案:1)把cell.photo.layer.cornerRadius = cell.photo.bounds.size.width / 2; into

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

2) Create custom class for your UICollectionCell and override layoutSubviews function: 2)为您的UICollectionCell创建自定义类并覆盖layoutSubviews函数:

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

This helped me a lot. 这对我帮助很大。

In Subclass of UICollectionViewCell, CustomCell.m: 在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];
}

为了让你的图像看起来像一个圆圈,你必须确保你的框架是一个完美的正方形,并将角落半径设置为或多或少60.这对我有用,希望对你有帮助!

Your cornerRadius value is correct but cell.photo.layer.masksToBounds = NO; 你的cornerRadius值是正确的但是cell.photo.layer.masksToBounds = NO; which in incorrect set it to cell.photo.layer.masksToBounds = YES; 哪个错误设置为cell.photo.layer.masksToBounds = YES; and if its still not working then make sure that the height and the width of your ImageView should be same. 如果仍然无法正常工作,请确保ImageView的高度和宽度应相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM