Reputation: 1754
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
}
Upvotes: 1
Views: 1397
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
Reputation: 166
Override layoutSubviews() inside your collection view cell class and set corner radius inside it.
Upvotes: 2
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