[英]Swift Tapping on UIButton without response
addButton = UIButton(type: .custom)
addButton.setTitle("add", for: .normal)
addButton.setTitle("tapped", for: .highlighted)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
addButton.backgroundColor = UIColor.lightGray
addButton.translatesAutoresizingMaskIntoConstraints = false
addButton.addTarget(self, action: #selector(dosome(_:)), for: .touchUpInside)
let bottom_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.bottom, relatedBy:NSLayoutRelation.equal, toItem:mainView, attribute:NSLayoutAttribute.bottom, multiplier:1.0, constant: -10)
let right_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.right, relatedBy:NSLayoutRelation.equal, toItem:mainView, attribute:NSLayoutAttribute.right, multiplier:1.0, constant: -20)
let height_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.height, relatedBy:NSLayoutRelation.equal, toItem:nil, attribute:.notAnAttribute, multiplier:1.0, constant: 20)
let width_button: NSLayoutConstraint = NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.width, relatedBy:NSLayoutRelation.equal, toItem:nil, attribute:.notAnAttribute, multiplier:1.0, constant: 35)
addButton.addConstraint(height_button)
addButton.addConstraint(width_button)
mainView.addSubview(addButton)
mainView.addConstraint(bottom_button)
mainView.addConstraint(right_button)
@IBAction func dosome(_ sender: UIButton) {
print("tttttt")
}
When tapped, button's text turn to highlighted title without action. 点击后,按钮的文本将变为突出显示的标题,而无需进行操作。 Anyone can point out what's wrong?
任何人都可以指出出什么问题了吗?
使用addButton.isHighlighted = false
它将起作用。
试试这个, addButton = UIButton(type: .system)
theButton.adjustsImageWhenHighlighted = false;
addButton = UIButton(type: .system)
并设置theButton.adjustsImageWhenHighlighted = false;
I just tried this out and it works fine. 我只是尝试了一下,效果很好。 Removed the constraints, you can add those according to your requirement.
删除约束后,可以根据需要添加约束。 Make sure you don't have a tapGesture or userInteractionEnabled view above this addButton which is causing your IBAction method to not being called.
确保在此addButton上方没有tapGesture或userInteractionEnabled视图,这导致未调用IBAction方法。
let addButton = UIButton(type: .custom)
addButton.setTitle("add", for: .normal)
addButton.setTitle("tapped", for: .highlighted)
addButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
addButton.backgroundColor = UIColor.lightGray
addButton.translatesAutoresizingMaskIntoConstraints = false
addButton.addTarget(self, action: #selector(self.dosome(_:)), for: .touchUpInside)
self.view.addSubview(addButton)
func dosome(_ sender: UIButton) {
print("tttttt")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.