Appearance proxies / UI_APPEARANCE_SELECTOR in Swift?

后端 未结 4 2121
庸人自扰
庸人自扰 2020-12-25 10:06

The Apple documentation states:

To participate in the appearance proxy API, tag your appearance property selectors in your header with UI_APPEARANCE

4条回答
  •  青春惊慌失措
    2020-12-25 10:29

    Based on previous answers, here is the extension to UIView I implemented to manage UIView appearance with view borders, I hope it can help someone :

    extension UIView {
        @objc dynamic  var borderColor: UIColor? {
            get {
                if let color = self.layer.borderColor {
                    return UIColor(cgColor: color)
                } else {
                    return nil
                }
            }
            set(color) {
                self.layer.borderColor = color?.cgColor
            }
        }
    
        @objc dynamic  var borderWidth: NSNumber? {
            get { return NSNumber(value: Float(self.layer.borderWidth))}
            set(width) {
                self.layer.borderWidth = CGFloat(width?.floatValue ?? 0)
            }
        }
    
        @objc dynamic  var cornerRadius: NSNumber? {
            get { return NSNumber(value: Float(self.layer.cornerRadius))}
            set(radius) {
                self.layer.cornerRadius = CGFloat(radius?.floatValue ?? 0)
            }
        }
    }
    

提交回复
热议问题