Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime BorderColor is not working in Xcode 9

Tags:

xcode

ios

swift

I made a extension of CALayer for border color as a runtime attribute, but it is not working.

It shows default black color only.

extension CALayer {
    var borderUIColor: UIColor {
        set {
            self.borderColor = newValue.cgColor
        }

        get {
            return UIColor(cgColor: self.borderColor!)
        }
    }
}

enter image description here

like image 419
nirav Avatar asked Feb 17 '26 07:02

nirav


1 Answers

You should handle nil values with care, and may create an extension to UIView which declares the property as @IBInspectabe:

import UIKit

extension UIView {
    @IBInspectable var borderColor: UIColor? {
        get {
            if let color = layer.borderColor {
                return UIColor(cgColor: color)
            }
            else {
                return nil
            }
        }
        set { layer.borderColor = newValue?.cgColor }
    }
}

This makes it much easier to set the border color in Attribute Inspector.

EDIT: Your example works for me with Xcode 9.0 and 8.3.3 as well. Probably it was a bug in a beta version.

like image 63
clemens Avatar answered Feb 18 '26 22:02

clemens