【问题标题】:UIButton is not highlighted when tap点击时 UIButton 未突出显示
【发布时间】:2017-05-20 03:32:22
【问题描述】:

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

观看此视频:http://sendvid.com/35e7za0p

这是我的代码

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)

}

【问题讨论】:

  • 使用 btnfirst.showsTouchWhenHighlighted = true 突出显示触摸。
  • @RamkrishnaSharma 感谢您的回复,但我需要默认选择的事件,当没有 TouchWhenHighlighted 的图像时出现
  • "我只是在顶部栏中添加按钮" 这没有意义。您不能将 UIButton 直接放入顶部栏中。而且我没有看到任何代码可以让您使用btnfirst 做任何事情。请展示它是如何进入界面的。
  • @matt 我没有使用 UINavigationBar。我用视图制作了顶栏。请观看视频

标签: ios swift uibutton highlight


【解决方案1】:

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

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

func btnTopbarFilter_Clicked(_ sender: UIButton) {

sender.isSelected = !sender.isSelected

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-30
    • 2013-07-05
    • 2014-03-10
    • 2020-05-06
    • 2017-07-06
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多