【发布时间】: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