0

I have this UINavigationBarButton I am making with an image inside of it that is originally square. I have used this code on other regular buttons and it has made the images round. However, it is not working for this button and I have no idea why. Also, I do not know how to make it smaller. (but don't mind its placement)

func preloadMe() {

    let btn1 = UIButton(type: .custom)
    let completelink = self.me[0].picture_url
    self.loadProfilePhoto(image: btn1, link: completelink)
    btn1.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
    btn1.layer.cornerRadius = 0.5 * btn1.frame.height
    //btn1.setImage(UIImage(named: "profileMenuButton"), for: .normal)
    btn1.addTarget(self, action: #selector(HomeViewController.menuClicked), for: .touchUpInside)
    let item1 = UIBarButtonItem(customView: btn1)
    btn1.contentEdgeInsets = UIEdgeInsetsMake(0, -70, -10, 0)
    self.navigationItem.setLeftBarButtonItems([item1], animated: true)

}

   func loadProfilePhoto(image: UIButton, link: String) {
    image.downloadedFrom2(link: link)
    image.imageView!.clipsToBounds = true
    image.imageView!.layer.cornerRadius = (image.imageView!.frame.height) / 2
    image.imageView!.contentMode = .scaleAspectFill
    }

As you can see in the top left it is still square.

After a suggestion of adding in btn1.backgroundColor = .black I am now getting this result. What should I do now?

The new result

Levi K
  • 573
  • 1
  • 4
  • 23

1 Answers1

0

Your button background color is nil. That's why drawing context is not visible. Set your button background color and your problem will be solved.

btn1.backgroundColor = .black
Muzahid
  • 5,072
  • 2
  • 24
  • 42
  • Ok, it is somewhat on the right track. I am uploading a photo of what it looks like now, the photo is completely separated from the button and is still square, but the button is round. I will edit the original post with the new photo – Levi K Jul 08 '18 at 15:16
  • why do you set imageView of the button rather than setImage or setBackgorundImage? – Muzahid Jul 09 '18 at 06:29