[英]Swift - UIButton not highlighted while pressed
以编程方式添加UIButton
时遇到问题。 没有“点击动画”。
有谁知道为什么会发生这种情况以及如何解决这个问题? 我找不到有关此主题的任何内容...
let weiterButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.setTitle("WEITER", for: .normal)
v.titleLabel?.font = UIFont(name: "AvenirNext-Bold", size: 20)
v.titleLabel?.textColor = .white
v.backgroundColor = UIColor(red: 75/255, green: 75/255, blue: 75/255, alpha: 1)
v.addTarget(self, action: #selector(weiterButtonTapped), for: .touchUpInside)
return v
}()
我想要的动画:
您必须设置lazy var
而不是let
因为您想退出变量(按钮)。 此外,您必须设置UIButton(type: .system)
因为您告诉该按钮应该具有某种样式。
lazy var weiterButton: UIButton = {
let v = UIButton(type: .system)
v.translatesAutoresizingMaskIntoConstraints = false
v.setTitle("WEITER", for: .normal)
v.titleLabel?.font = UIFont(name: "AvenirNext-Bold", size: 20)
v.setTitleColor(.white, for: .normal)
// If you want a different color when it is pressed
v.setBackgroundImage(UIImage(named: "Hlighlighted image"), for: .highlighted)
v.setBackgroundImage(UIImage(named: "Your image"), for: .normal)
v.addTarget(self, action: #selector(weiterButtonTapped), for: .touchUpInside)
return v
}()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.