繁体   English   中英

UIButton在水龙头Swift上不突出显示

[英]UIButton not highlighting on tap Swift

介绍

我创建了一个按钮并为其添加了子视图/为其提供了自定义外观。

由于子类化按钮非常麻烦,因此我决定在视图控制器内部进行操作(是的,我知道这很不好),并为UIButton创建了一个扩展,以样式创建按钮。

该按钮如下所示:

我的按钮

我将子视图设置如下:

func styleCreateButton(serviceFee: Float) {
        let feeLabel: DefaultLabel = {
            let lbl = DefaultLabel(labelFont: UIFont(name: "MyCustomFont", size: 12)!, labelTextColor: ColorCodes.textSuperLightGray, labelText: "$\(serviceFee) Service Fee")

            return lbl
        }()

        let createLabel: DefaultLabel = {
            let lbl = DefaultLabel(labelFont: UIFont(name: "MyCustomFont", size: 15)!, labelTextColor: .white, labelText: "Create")

            return lbl
        }()

        let arrowRight: UIButton = {
            let btn = UIButton(type: .system)
            btn.setImage(UIImage(named: "left-arrow")?.rotate(radians: CGFloat(180).degreesToRadians).resizedImage(newSize: CGSize(width: 20, height: 20)).withRenderingMode(.alwaysTemplate), for: .normal)
            btn.contentMode = .center
            btn.tintColor = .white

            return btn
        }()

        self.addSubview(feeLabel)
        feeLabel.anchor(top: nil, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 16, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
        feeLabel.sizeToFit()
        feeLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

        self.addSubview(arrowRight)
        arrowRight.anchor(top: nil, left: nil, bottom: nil, right: self.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 16, width: 20, height: 20)
        arrowRight.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

        self.addSubview(createLabel)
        createLabel.anchor(top: nil, left: nil, bottom: nil, right: arrowRight.leftAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 12, width: 0, height: 0)
        createLabel.sizeToFit()
        createLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

该按钮本身声明如下:

private lazy var createButton: UIButton = {
        let btn = UIButton(type: .system)

        btn.backgroundColor = ColorCodes.logoPrimaryColor

        btn.layer.shadowOffset = CGSize(width: 0, height: 5)
        btn.layer.shadowColor = UIColor.black.cgColor
        btn.layer.shadowOpacity = 0.2
        btn.layer.shadowRadius = 5

        return btn
    }()

另外,该按钮有一个目标,我不知道为什么单击时按钮没有以典型的iOS方式突出显示。 现在,处理程序函数被调用,但是该按钮未突出显示。

我将不胜感激任何帮助 :)

尝试这样做

class HighlightedButton: UIButton {
    override var isHighlighted: Bool {
        didSet {
            backgroundColor = isHighlighted ? .blue : .white
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM