簡體   English   中英

在UIButton上快速點擊無響應

[英]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")
}

點擊后,按鈕的文本將變為突出顯示的標題,而無需進行操作。 任何人都可以指出出什么問題了嗎?

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

試試這個, addButton = UIButton(type: .system) theButton.adjustsImageWhenHighlighted = 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")
      }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM