I have a custom cell, which is subclass of
UITableViewCell
Cell
UITableViewCell
Cell
xib
self.tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "CellIdentifier")
UIView
UIImageView
Cell
xib
Autolayout
ViewController
cellForRowAtPath
willDisplayCellForRowAtPath
cell.imageView.layer.cornerRadius = cell.imageView.layer.frame.height / 2.0
cell.imageView.layer.masksToBounds = true
cell.containerView.layer.cornerRadius = cell.containerView.layer.frame.height / 2.0
cell.containerView.layer.borderColor = UIColor.lightGray.cgColor
cell.containerView.layer.borderWidth = 1
UITableView's insertRows(atIndexPaths)
func addCell(_ title: String) {
titles.append(title)
let indexPath = IndexPath(row: messages.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .bottom)
tableView.endUpdates()
tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
Talked with you about this outside of StackOverflow, posting it here for others benefit.
I created a demo project that has the same issue you mentioned. I found a workaround by subclassing the cell and overriding layoutSubviews and setting the cornerRadius
using DispatchQueue.main.async {}
:
class TestCell: UITableViewCell {
@IBOutlet weak var colorImageView: UIImageView!
override func layoutSubviews() {
super.layoutSubviews()
DispatchQueue.main.async {
self.colorImageView.layer.cornerRadius = min(self.colorImageView.frame.width, self.colorImageView.frame.height) / 2
}
}
}
Another workaround I found was by using an observer(Swift 4) and a layoutIfNeeded()
call before setting the cornerRadius
class TestCell: UITableViewCell {
@IBOutlet weak var colorImageView: UIImageView!
var observer: NSKeyValueObservation?
override func awakeFromNib() {
super.awakeFromNib()
observer = self.observe(\.frame) { (cell, change) in
self.layoutIfNeeded()
self.colorImageView.layer.cornerRadius = min(self.colorImageView.frame.width, self.colorImageView.frame.height) / 2
}
}
}