为什么这个UIButton不能四舍五入Swift?

时间:2018-07-08 04:41:31

标签: swift uibutton uibarbuttonitem uiedgeinsets

我有一个正在制作的UINavigationBarButton,其中的图像原本是正方形的。我在其他常规按钮上使用了此代码,它使图像变圆了。但是,此按钮不起作用,我也不知道为什么。另外,我不知道如何缩小尺寸。 (但不要介意其位置)

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.

在建议添加btn1.backgroundColor = .black 之后,我现在得到了这个结果。我现在该怎么办?

The new result

1 个答案:

答案 0 :(得分:0)

您的按钮背景色为零。这就是为什么绘图上下文不可见的原因。设置按钮背景色,您的问题将得到解决。

btn1.backgroundColor = .black