Yogesh Tandel
Yogesh Tandel

Reputation: 1754

Image in collection view cell inside of uitableviewcell does not take rounded corners

I have a tableview and in one of the rows, I have a custom cell which has a collection view with horizontal scroll. In the collection view cell I have the Image for which I have added rounded corners.

img_Photo.backgroundColor = UIColor.colorFromCode(0xf2f2f2)
img_Photo.layer.cornerRadius = img_Photo.frame.height / 2
img_Photo.layer.masksToBounds = true

The corner radius shows as below first and then on scrolling it looks proper. What am I missing to set this properly.

I have added layoutIfNeeded in the tableviewcell

override func layoutIfNeeded() {
    super.layoutIfNeeded()
    collectView.frame = self.contentView.bounds
}

on first load

after scrolling

Upvotes: 1

Views: 1397

Answers (3)

Nikesh Hyanju
Nikesh Hyanju

Reputation: 190

Before returning your cell in cellForItemAtIndexPath method just add cell.layoutIfNeeded() before returning your cell.

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)


    cell.layoutIfNeeded()// this is to be added in your code
    return cell
}

Upvotes: 0

atul
atul

Reputation: 166

Override layoutSubviews() inside your collection view cell class and set corner radius inside it.

Upvotes: 2

M. Mansuelli
M. Mansuelli

Reputation: 1123

Create a class like this and use it in your xib Layout:

import UIKit

class RoundedView: UIView {

    override func layoutSubviews() {
        super.layoutSubviews()

        layer.cornerRadius = frame.height / 2
    }
}

Upvotes: 0

Related Questions