【问题标题】:Why is this UIButton not rounding Swift?为什么这个 UIButton 不四舍五入 Swift?
【发布时间】:2018-07-08 04:41:30
【问题描述】:

我正在制作这个 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
    }

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

【问题讨论】:

    标签: swift uibutton uibarbuttonitem uiedgeinsets


    【解决方案1】:

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

    btn1.backgroundColor = .black
    

    【讨论】:

    • 好的,它有点在正确的轨道上。我正在上传一张现在看起来像的照片,照片与按钮完全分开,仍然是方形的,但按钮是圆形的。我将用新照片编辑原始帖子
    • 为什么要设置按钮的imageView而不是setImage或者setBackgorundImage?
    猜你喜欢
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    相关资源
    最近更新 更多