I have been trying to create a circular image but for some reason it has been outputting as a diamond. This is my code:
override func viewDidLoad() {
display
I had the same problem but because I had copied and pasted the cell from another view, the cell class was giving it instructions meant for another cell.
The issue is that viewWillLayoutSubviews
is too early in the layout process. You can do it in viewDidLayoutSubviews
.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
profilePic.layer.cornerRadius = min(profilePic.frame.width, profilePic.frame.height) / 2
profilePic.clipsToBounds = true
}
Or, alternatively, you can use a designable view, and let the view take care of it:
@IBDesignable class RoundedImageView: UIImageView {
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = min(bounds.width, bounds.height) / 2
}
}
And by making it designable, you can see it rendered with rounded corners right in Interface Builder.
Your problem it is probably caused by some constraints you have added to your imageView. Try adding only 4 constraints, 1 for fixed width, 1 for fixed height, 1 for leading space and 1 for top space as you can see at the screenshot below: