自定义 UIButton imageEdgeInsets/titleEdgeInsets 不工作

Custom UIButton imageEdgeInsets/titleEdgeInsets not working

imegEdgeInsetstitleEdgeInsets 不适用于我的自定义 UIButton class。 我正在通过 nib

设置插图

.

自定义UIButtonclass代码

class FilledButton: kButton {
    private var bgColor = AppTonalPalette.color(.primary)
    private var bgColorPressed = AppTonalPalette.color(.primary).withAlphaComponent(0.88)
    private var foregroundColor = AppTonalPalette.color(.onPrimary)
    
    private var bgColorDisabled = UIColor.init("#1F1F1F", alpha: 0.12)
    private var foregroundColorDisabled = AppTonalPalette.color(.onSurface)
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updateViews()
    }
    
    func updateViews() {
        tintColor = isEnabled ? foregroundColor : foregroundColorDisabled
        setTitleColor(isEnabled ? foregroundColor : foregroundColorDisabled, for: .normal)
        setTitleColor(isEnabled ? foregroundColor : foregroundColorDisabled, for: .highlighted)
        backgroundColor = isEnabled ? (isHighlighted ? bgColorPressed : bgColor) : bgColorDisabled
    }
}

class kButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        setup()
    }
    
    func setup() {
        titleLabel?.font = AppFont.fontOf(size: 14, style: .Regular)
        setNeedsLayout()
    }
}

我也试过在 mu 自定义 class 代码中添加插图,例如:

    func updateViews() {
        tintColor = isEnabled ? foregroundColor : foregroundColorDisabled
        setTitleColor(isEnabled ? foregroundColor : foregroundColorDisabled, for: .normal)
        setTitleColor(isEnabled ? foregroundColor : foregroundColorDisabled, for: .highlighted)
        backgroundColor = isEnabled ? (isHighlighted ? bgColorPressed : bgColor) : bgColorDisabled
        
        if imageView?.image != nil {
            imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: 10)
            titleEdgeInsets = .init(top: 0, left: 10, bottom: 0, right: 0)
        }
    }

但还是一样。这里缺少什么?

尝试通过按钮属性检查器将按钮样式更改为默认样式。