-1

I have a UIButton and I wanna add a circle shape in the middle of it. how can I do that?

Here's what I want to achieve. enter image description here

Thanks/

Dark star
  • 5,192
  • 9
  • 35
  • 54

2 Answers2

1

There are many ways to do this including using CoreGraphics. The easiest way is to just set an image on the button

How to set image of UIButton in Swift 3?

If you need it clickable ofcourse, otherwise just use a UIImageView(which you can also make clickable using UITapGestureRecognizer)

Rajeev Bhatia
  • 2,974
  • 1
  • 21
  • 29
  • I want to change the circle shape to square later on base on tap, so I need be something dynamic, not an image. – Dark star Oct 06 '17 at 12:00
  • You can change set the selected and unselected image and just change the button selected, the image will change automatically – Rajeev Bhatia Oct 06 '17 at 12:00
  • https://developer.apple.com/documentation/uikit/uibutton/1623997-setimage set the image for both .normal and .selected and then toggle state on user tap – Rajeev Bhatia Oct 06 '17 at 12:01
  • https://stackoverflow.com/a/22883654/2585955 here's an example. i would edit the question to include this so that I can add this in my answer – Rajeev Bhatia Oct 06 '17 at 12:02
1

Insert a subview in your button

let circleFrame = CGRect(x: 0, y: 0, width: circleDimension, height: circleDimension)
let circle = UIView(frame: circleFrame)
circle.backgroundColor = UIColor.clear
circle.layer.borderWidth = circleWidth
circle.layer.borderColor = UIColor.white.cgColor
circle.layer.cornerRadius = circleDimension/2

Just add that as a subview to your button and set centerX and centerY anchors equal to your button. Then, if you want to make it square, just do

circle.layer.cornerRadius = 0
mag_zbc
  • 6,801
  • 14
  • 40
  • 62