图像出现在 UIImageView 圆角半径边界 swift

image appear over UIImageView corner radius border swift

当我将边框宽度和圆角半径设置为 UIImageView 并加载图像时,它出现在边框上方 如何解决这个问题

let viewedImage : UIImageView = {
    let img = UIImageView()
    img.layer.borderWidth = 1
    img.layer.borderColor = UIColor.lightGray.cgColor
    img.layer.cornerRadius = 28
    return img
}()

设置Docs

img.clipsToBounds = true

Setting this value to true causes subviews to be clipped to the bounds of the receiver. If set to false, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is false

根据Apple Documentation

clipsToBounds property

A Boolean value that determines whether subviews are confined to the bounds of the view.

Discussion Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.

let viewedImage : UIImageView = {
        let img = UIImageView()
        img.layer.borderWidth = 1
        img.layer.borderColor = UIColor.lightGray.cgColor
        img.layer.cornerRadius = 28
        img.clipsToBounds = true
        return img
    }()

尝试一下,告诉我它是否有效...

let viewedImage : UIImageView = {

        let img = UIImageView()
        img.layer.cornerRadius = 28
        img.layer.clipsToBounds = true
        img.layer.borderWidth = 2
        img.layer.borderColor = UIColor.lightGray.cgColor
        return img
    }()