简体   繁体   中英

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.

        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")
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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