快速点击UIButton没有回应

  • 本文关键字:回应 UIButton ios swift
  • 更新时间 :
  • 英文 :

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

点击时,按钮的文本将变为突出显示的标题,无需执行任何操作。 任何人都可以指出出了什么问题?

使用addButton.isHighlighted = false它会起作用。

试试这个,addButton = UIButton(type: .system)并设置theButton.adjustsImageWhenHighlighted = false;

我刚刚尝试过这个,它工作正常。删除了约束,您可以根据需要添加它们。确保在此 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")
      }

最新更新