[英]UIButton not highlighting on tap Swift
I created a button and added subviews to it / gave it a custom look. 我创建了一个按钮并为其添加了子视图/为其提供了自定义外观。
As subclassing buttons is a lot of pain, I decided to do it (yeah, I know it's bad) inside my view controller and creating an extension for UIButton which styles create buttons. 由于子类化按钮非常麻烦,因此我决定在视图控制器内部进行操作(是的,我知道这很不好),并为UIButton创建了一个扩展,以样式创建按钮。
The button looks like this: 该按钮如下所示:
I set up the subviews as follows: 我将子视图设置如下:
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
}
The button itself was declared as follows: 该按钮本身声明如下:
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
}()
Also, the button has a target and I have no idea why it does not highlight in the typical iOS way when clicking. 另外,该按钮有一个目标,我不知道为什么单击时按钮没有以典型的iOS方式突出显示。 Right now, the handler function gets called, but the button does not highlight.
现在,处理程序函数被调用,但是该按钮未突出显示。
I would appreciate any help :) 我将不胜感激任何帮助 :)
try to do this 尝试这样做
class HighlightedButton: UIButton {
override var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? .blue : .white
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.