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
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.