点按时不会突出显示UIButton

时间:2017-01-05 14:32:30

标签: ios swift uibutton highlight

我知道有很多与此主题相关的答案,但我尝试了一切,根本不明白代码中出了什么问题。我只是在顶部栏中添加按钮并尝试点击但没有高亮动画触发器。

观看此视频:try_convert

这是我的代码

if rightFisrtBtn != nil {
    btnfirst = UIButton(type: UIButtonType.custom)
    btnfirst.tag = tagTopBar.btnfirst.rawValue
    btnfirst.translatesAutoresizingMaskIntoConstraints = false
    btnfirst.imageView?.contentMode = UIViewContentMode.scaleAspectFill
    btnfirst.titleLabel?.font = fontWithSize


    if rightFisrtBtn! is String {
        btnfirst.setTitle(rightFisrtBtn as? String, for: .normal)
        btnfirst.setTitleColor(UIColor.black, for: .normal)
        btnfirst.setTitleColor(UIColor.white, for: .highlighted)

        topView.addSubview(btnfirst)

        topView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[btnfirst(width)]-[btnSecond]", options: [], metrics: ["width":btnfirst.intrinsicContentSize.width + 10.0], views: ["btnfirst":btnfirst,"btnSecond":btnSecond]))

        topView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(25.0)-[btnfirst]-(gap)-|", options: [], metrics: ["gap":10.0], views: ["btnfirst":btnfirst]))

    } else if rightFisrtBtn! is UIImage {
        btnfirst.setImage(rightFisrtBtn as? UIImage, for: .normal)
        btnfirst.contentHorizontalAlignment = UIControlContentHorizontalAlignment.center
        topView.addSubview(btnfirst)

        topView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[btnfirst(width)]-[btnSecond]", options: [], metrics: ["width":btnfirst.imageView?.image?.size], views: ["btnfirst":btnfirst,"btnSecond":btnSecond]))

        topView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(25.0)-[btnfirst]-(gap)-|", options: [], metrics: ["gap":5.0], views: ["btnfirst":btnfirst]))
    }

    btnfirst.addTarget(self, action: #selector(self.btnTopbarFilter_Clicked(_:)), for: .touchUpInside)

}

1 个答案:

答案 0 :(得分:-1)

如果我能理解您的查询,您需要在点按按钮时显示按钮突出显示状态的属性。

在这种情况下,在按钮的target-action实现中使用UIButton实例的isSelected属性:

func btnTopbarFilter_Clicked(_ sender:UIButton){

sender.isSelected = !sender.isSelected

}