Kolby O'Malley Kolby O'Malley -5 years ago 279
Swift Question

How to make UIButton a circle?

I have been trying to make the UIButton in the cells a perfect circle. Unfortunately the circle has been formed based on the background image rather than the UIButton frame.

The code I have for creating a circle:

cell.StoryViewButton.setImage(image, forState: .Normal)
cell.StoryViewButton.frame = CGRectMake(50, 8, 100, 100)
cell.StoryViewButton.layer.masksToBounds = false
cell.StoryViewButton.layer.cornerRadius = cell.StoryViewButton.frame.width/2
cell.StoryViewButton.clipsToBounds = true

The output looks like this: Output Images for each cell

What can I do to get the perfect circle button frames that I want?

Answer Source

Try something like this

 cell.StoryViewButton.layer.masksToBounds = true
 cell.StoryViewButton.layer.cornerRadius = cell.StoryViewButton.frame.width/2

If you need to create a circle of view you have to set masksToBounds to true, do not set clipsToBounds

Hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download