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")
}
點擊時,按鈕的文本將變爲突出顯示的標題而不動作。任何人都可以指出什麼是錯的?在沒有迴應的情況下在UIButton上快速點擊
MainView是一個顯示在彈出窗口中的UIView。 isUserInteractionEnabled的Button和它的超級視圖返回true。 – uuuy1798