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

前端 未结 8 2509
你的背包
你的背包 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:06

    Swift 5.0:

    My personal preference is to have an extra swift file for specific changes like this one. What I do is then create a class e.g. "RoundCorner" which is a subclass of the element I want to change in this case a View element. And then I am overriding the individual settings.

    class RoundCorner: UIView {
    override func draw(_ rect: CGRect) {
        self.layer.cornerRadius = 10 // change this number to get the corners you want
        self.layer.masksToBounds = true
        }
    }
    

    After that, you only have to select the element you want this changes on, and set the custom class to the class we created earlier.

    Look at the screenshot here

提交回复
热议问题