-2

I have a button in UIViewcontroller which contains the button with name and image. I want to make UIImageView circular that is in this button [NOT WHOLE BUTTON].

Is there any way to do that?

here is a screenshot of current state :

enter image description here

what I want is :

enter image description here


"Similar question" (How different than mine):

How can I set UIButton image, which Image is round shape with a border

I do not want the border to image view and also I have taken button in the storyboard and only want to make an image circular (without border any border).

it is pretty clear that the question why my question is marked duplicate for is only setting the border to button not making UIImageView circular.

he is asking for programmatically created button and setting border so that button could look circular and also the answer is not accepted and none of the answers is working for me.

and I like to point out that as you can see I have accepted one of the below answer the person have given the answer as I expected.

and also because it's in navigation bar resulting in changing the size of a button but because the answer it's not giving me an error like this question

How to create a circular button in Swift?

again for those who flagged duplicate for the round button, I do not want the button to be round.


NickCoder
  • 1,504
  • 2
  • 23
  • 35

3 Answers3

3

Set the cornerRadius of button.imageView's to half of button's height in viewDidLayoutSubviews(), i.e.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: button.bounds.width - button.bounds.height)
    button.imageView?.layer.cornerRadius = button.bounds.height/2.0
}

I've set the imageEdgeInsets of the button so you can get the exact circular image.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • but rather than setting code in viewDidLayoutSubviews() i set in viewdidload() does that make any difference? – NickCoder Jul 03 '19 at 12:13
  • You won't get the right `frame` of `button` in `viewDidLoad()`. That's why I add the code in `viewDidLayoutSubviews()`. You can move it to `viewDidAppear(_:animated:)` as well. – PGDev Jul 03 '19 at 12:13
  • but I am getting the exact output as I needed so what will you suggest should i add code in viewDidLayoutSubviews() – NickCoder Jul 03 '19 at 12:15
  • This is because you might be using the same simulator as in the storyboard. That's why it might be taking the same frames. – PGDev Jul 03 '19 at 12:16
0

I would suggest creating an custom button and add an UIImageView as a subview, make the correct size and set layer.cornerRadius = height/2

Vollan
  • 1,887
  • 11
  • 26
-1

Swift 4.2:

let image = UIImage(named: "imageName")
imageView.layer.borderWidth = 1.0
imageView.layer.masksToBounds = false
imageView.layer.borderColor = UIColor.white.cgColor
imageView.layer.cornerRadius = image.frame.size.width / 2
imageView.clipsToBounds = true
Zouhair Sassi
  • 1,403
  • 1
  • 13
  • 30