Kolby O'Malley Kolby O'Malley - 4 months ago 33x
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?


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.