How do I make an UIImage/-View with rounded corners CGRect (Swift)

前端 未结 8 2519
你的背包
你的背包 2020-12-13 01:47

How do I make a UIImageView with rounded corners on a Swift iOS Playground?
Inside it needs to be filled with a color.

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 02:02

    I was tired of writing set radius and mask to bound for each UIView. So I made the following extenstion for UIView. Should work for every UIView subclass, though I have not tested it. The extension can be narrowed down for specific Views you use of course.

    extension UIView {      
        func setRadius(radius: CGFloat? = nil) {
            self.layer.cornerRadius = radius ?? self.frame.width / 2;
            self.layer.masksToBounds = true;
        }
    }
    

    It will default to the view's half width if you don't pass it any specific value.

提交回复
热议问题