如何拥有带背景图片的圆形按钮 (Swift 4)

How to have a circular button with background image (Swift 4)

我有一个圆形按钮,我想通过照片库显示图像,但是当我给它一个背景图像时,它又变回正方形。我需要知道如何让按钮既是圆形又包含背景图像。

使用

YourbuttonObject.layer.cornerRadius = YourbuttonObject.frame.height/2
YourbuttonObject.clipsToBounds = true

现在当你设置一个 backgroundImage 它将是循环的

或者您可以使用 ImageView 并将其用作按钮并为其设置 backgroundImage 并执行

YourImageObject.layer.cornerRadius = YourImageObject.frame.height/2
YourIamgeObject.clipsToBounds = true