Sourav Sachdeva Sourav Sachdeva - 7 months ago 11
Swift Question

Getting round Button With User Defined Runtime Attributes

First i'm working with swift . Second i have made the CAlayer extension

extension CALayer
{
func borderUIColor() -> UIColor? {
return borderColor != nil ? UIColor(CGColor: borderColor!) : nil
}

func setBorderUIColor(color: UIColor) {
borderColor = color.CGColor
}
}


and here are the results with this attributes

enter image description here


results:


enter image description here

I get those corner kind of spikes which of the layers properties can help me. Do I Shift bounds or deal with content or something.

Answer

Replace your extension methods with a computed property, like so:

extension CALayer
{
    var borderUIColor(): UIColor? {
        get {
            return borderColor
        }

        set {
            borderColor = borderUIColor
        }
    }
}

As you can see, you really don't need an extension at all--just use the layer's borderColor. Replace your layer.borderUIColor runtime attribute with layer.borderColor, and, for best effect, add layer.masksToBounds as true.