Circular image is square on first table refresh
Circular image is square on first table refresh
I have a search bar connected to my UITableView
.
UITableView
Here is my code so far:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = SongPicker.dequeueReusableCell(withIdentifier: "cell")!
// Image
cell.imageView?.image = songHandler.songItems[indexPath.row].songImage?.cropToBounds()
cell.imageView?.layer.cornerRadius = cell.imageView!.frame.width / 2
cell.imageView?.layer.masksToBounds = true
cell.imageView?.layer.borderWidth = 3
cell.imageView?.layer.borderColor = UIColor.lightGray.cgColor
// Song name
cell.textLabel?.text = songHandler.songItems[indexPath.row].songName
// Artist name
cell.detailTextLabel?.text = songHandler.songItems[indexPath.row].songArtist
return cell
}
(SongPicker
is my table)
SongPicker
By printing out the result of cell.imageView?.layer
, I can see that when the images first load, they do not have the cornerRadius
property set within the instance. After they reload properly, the property is set.
cell.imageView?.layer
cornerRadius
However, when the cells move out of view or updated via getting new information (.reloadData()
does nothing), the images in the cells are circular.
.reloadData()
There are other question about this, but everyone repeats that you need to set the cornerRadius
property and the masksToBounds
. This, in no way, would help solve my problem as those properties are clearly set.
cornerRadius
masksToBounds
I have tried making an extension to a UITableViewCell
with the image layer code as well, but it has the exact same results.
UITableViewCell
Why are my images starting off square, and how can I easily fix this?
If you have any question please ask
cornerRadius
layer.cornerRadius
Number
dequeueReusableCell(withIdentifier:)
nil
dequeueReusableCell(withIdentifier:, for:)
UITableViewCell
1 Answer
1
Actually the imageView's frame
is .zero
at the moment when you are setting the cornerRadius
of its layer. If you have fix size for the imageView
then you can set cornerRadius
as half of the width
and it will work fine. Something as below,
imageView's frame
.zero
cornerRadius
imageView
cornerRadius
width
cell.imageView?.layer.cornerRadius = 50 //If width&height is 100 fix.
Second option is to set the correct cornerRadius
by overriding
the layoutSubviews
method because this method ensures that your cell
has laid out its size and you will get correct frame
for subViews
cornerRadius
overriding
layoutSubviews
cell
frame
subViews
class MyTableViewCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.layer.cornerRadius = self.imageView!.frame.width / 2
self.imageView?.layer.masksToBounds = true
self.imageView?.layer.borderWidth = 3
}
}
Also, just to note, I did the second option
– George_E_2
Jun 29 at 19:46
Yes, that is more cleaner as you are putting cell specific code in its own class.
– Kamran
Jun 29 at 19:48
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Did you try setting your
cornerRadius
in the Storyboard (settinglayer.cornerRadius
of typeNumber
in the User Dedined Runtime Attributes)? Also, don't usedequeueReusableCell(withIdentifier:)
as it could returnnil
and crash your app due to the forced unwrapping. UsedequeueReusableCell(withIdentifier:, for:)
which ensures an actualUITableViewCell
.– Alejandro Iván
Jun 29 at 19:25