Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make images round in swift?

Tags:

ios

swift

xcode6

I'm trying to make images circular and for some reason the below code is creating a diamond-shaped photo:

    profilePicture.layer.cornerRadius = profilePicture.frame.size.width / 2
    profilePicture.clipsToBounds = true

How do I make it round? Thanks!

like image 812
Kody R. Avatar asked Jul 07 '15 19:07

Kody R.


1 Answers

It's displaying a diamond shape because you're setting the cornerRadius before the the size of the view changes.

This would result in a diamond shape:

var myView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
myView.backgroundColor = UIColor.redColor()
view.addSubview(myView)
myView.layer.cornerRadius = myView.frame.size.width / 2
// setting frame doesn't change corner radius from the former large value
myView.frame = CGRect(x: 50, y: 50, width: 50, height: 50)

You can set this immediately before the view is displayed by doing so in viewWillAppear:

like image 72
Chris Slowik Avatar answered Oct 03 '22 19:10

Chris Slowik