【问题标题】:Set title color of all UIbuttons on button click设置按钮单击时所有 UIbuttons 的标题颜色
【发布时间】:2020-07-11 21:09:43
【问题描述】:

我设置了按钮的标题颜色

button.setTitle("Print 0", for: UIControl.State.normal)

但是我有很多按钮,如何一次设置它设置半径和边框工作但设置标题有一些问题

@objc extension UIButton {
    dynamic var borderColor: UIColor? {
        get {
            if let cgColor = layer.borderColor {
                return UIColor(cgColor: cgColor)
            }
            return nil
        }
        set { layer.borderColor = newValue?.cgColor }
    }

    dynamic var borderWidth: CGFloat {
        get { return layer.borderWidth }
        set { layer.borderWidth = newValue }
    }

    dynamic var cornerRadius: CGFloat {
        get { return layer.cornerRadius }
        set { layer.cornerRadius = newValue }
    }
      dynamic var setTitleColor: UIButton? {
        get {
            if let cgColor = layer.color {
                return UIColor(cgColor: cgColor)
            }
            return nil
        }
        set { layer.setTitleColor = newValue?.cgColor }
    }

}

【问题讨论】:

    标签: xcode xcode8 swift5


    【解决方案1】:

    像这样使用titleColor(for:)setTitleColor(, for:) 而不是layer.color:

    extension UIButton {
        dynamic var normalTitleColor: UIColor? {
            get {
                return titleColor(for: .normal)
            }
            set {
                setTitleColor(newValue, for: .normal)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      相关资源
      最近更新 更多