Amais Sheikh
Amais Sheikh

Reputation: 463

clipsToBounds / masksToBounds not working for image view in table view cell?

So I had done it several times, it worked fine, but i don't know what am I missing this time. The Problem is that image inside image view is not being clipped to bounds.

In simulator it is shown like this:

enter image description here

Inside the table view cell class, in my awakeFromNib() method i have following code:

override func awakeFromNib()
{
        super.awakeFromNib()
        
        profileImageView.layer.masksToBounds = true
        profileImageView.clipsToBounds = true
        profileImageView.layer.cornerRadius = profileImageView.frame.size.width / 2
        
        profileImageView.layer.borderWidth = 2
        profileImageView.layer.borderColor = UIColor.black.cgColor
}

but still as you see in the image, the image inside image view is coming out of the bounds, whereas circular border is correctly applied.

I have tried to put this code inside layoutSubviews() method in tableViewCell class, as well as i have also tried to apply this cornerRadius inside cellForRowAt method of table view. Moreover, i have also tried changing the properties of image view to Aspect Fit and Scale To Fit from storyboard.

Nothing is working, The image is still shown outside the circular bounds! why ?

EDIT: Screenshot of my storyboard, where i had a prototype cell, with image view in it

enter image description here

Upvotes: 1

Views: 527

Answers (1)

DonMag
DonMag

Reputation: 77700

As per the OP...

In cellForRowAt you're setting:

cell.imageView?.image

where you should be setting:

cell.profileImageView.image

A UITableViewCell has "built-in" .textLabel, .detailTextLabel and .imageView elements. We have to be careful not to use those objects if we've created a custom cell with our own labels / imageViews / etc.

Upvotes: 2

Related Questions