UIImageViewを次のようにextensionする。masksToBounds、cornerRadius、clipsToBoundsの3つを設定する。
import UIKit
extension UIImageView {
func circle() {
layer.masksToBounds = false
layer.cornerRadius = frame.width/2
clipsToBounds = true
}
}
レイヤーだけでなくclipsToBoundsを忘れないことがポイント。
あとは角丸にしたいUIImageViewについて
v = UIImageView(frame: CGRect(x: 8, y: 8, width: 48, height: 48))
v.circle()
などとする。