Mimma
Mimma

Reputation: 113

UIImage circle in UITableViewCell

So I'm following the the normal approach in Turing an image into circle :

   image.layer.cornerRadius = image.frame.size.width/2.0f;
    image.layer.borderColor = [UIColor blackColor].CGColor;
    image.layer.borderWidth = 2;
    image.clipsToBounds = YES ;

However when I used it in cells, first time it shows in Simi-perfect circle, but when I scroll to show new cells , all will be in perfect circle shape.
so my question is : why the first visible cells appear in a Simi-cirlce shape ? enter image description here This is how it looks like first time , but if I refresh or reload the page, everything is fine

Upvotes: 1

Views: 90

Answers (2)

Milan Nosáľ
Milan Nosáľ

Reputation: 19757

Override layoutSubviews in the cell and after calling super.layoutSubviews update the image.layer.cornerRadius there. Then when the layout of the cell is updated after the cell gets visible, the corner radius will be updated accordingly.

So in swift, in your cell implementantion you would do something like:

override func layoutSubviews() {
    super.layoutSubviews()

    // now the frames were recalculated, and we can update cornerRadius
    image.layer.cornerRadius = image.bounds.size.width / 2.0
}

Upvotes: 3

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

Try to use this way

like in custom cell class

class ProfilePicCell: UITableViewCell {

    @IBOutlet weak var image: UIImageView!

    override func awakeFromNib() {
        super.awakeFromNib()
        image.layer.cornerRadius =  image.frame.width / 2
        image.layer.masksToBounds = true
    }
}

in Objective C add this method in custom cell class

- (void)awakeFromNib
{
 super.awakeFromNib()

 image.layer.cornerRadius =  image.frame.width / 2
}

Upvotes: 3

Related Questions