Swift - 设置嵌套在UIButton中的图像的角半径

Fua*_*oro 3 uibutton swift

我有一个包含文本和图像的按钮,但我希​​望图像上有一个角半径.当我尝试将角半径应用于按钮时,它适用于我认为正确的整个按钮.如何将角半径设置为图像?

这是我的代码:

let titleButton = UIButton()
titleButton.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
titleButton.setTitle("display name", for: .normal)
titleButton.setImage(#imageLiteral(resourceName: "imgName"), for: .normal)
titleButton.layer.masksToBounds = true
titleButton.layer.cornerRadius = titleButton.frame.width / 2

self.navigationItem.titleView = titleButton
Run Code Online (Sandbox Code Playgroud)

Fan*_*ing 10

一个UIButton有一个UIImageView就可以了.因此,要设置角,只需将半径设置为图像视图的图层

titleButton.imageView.layer.cornerRadius = 5
Run Code Online (Sandbox Code Playgroud)