简而言之,如何更改黑色按钮项目的颜色(同时保持对其大小的控制)?
更长的版本:我以编程方式将一些自定义UIBarButtonItem添加到UIToolbar . 我正在使用the solution found here,以便我可以控制项目的大小 .
虽然这解决了UIBarButtonItem大小问题,但它不像典型的UIBarButtonItem那样尊重tintColor . 所以我得到这样的东西:
大白色项目是我想要的颜色,但不是大小 . 这个项目只是在IB中添加,tintColor设置为默认值 .
小黑色项目是我想要的大小,并添加了以下代码(N.B.标有//不生成有意结果的行):
for e in (self.profile?.expressions)! {
let button = UIButton()
button.setImage(UIImage(named: "emojiph"), for: .normal)
button.addTarget(self, action: #selector(onEmojiInsert), for: .touchUpInside)
let barItem = UIBarButtonItem(customView: button)
barItem.tag = e.family_expression_id
let wConstraint = barItem.customView?.widthAnchor.constraint(equalToConstant: 32)
wConstraint?.isActive = true
let hConstraint = barItem.customView?.heightAnchor.constraint(equalToConstant: 32)
hConstraint?.isActive = true
// Not producing intented result
button.tintColor = UIColor.white
// Not producing intented result
barItem.customView?.tintColor = UIColor.white
// Not producing intented result
barItem.tintColor = UIColor.white
self.emojibar.items?.append(barItem)
// Add flexible spacers
if (e.family_expression_id < (self.profile?.expressions.count)!) {
self.emojibar.items?.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil))
}
}
解决方法是提供白色图像资源,但如果存在,我更愿意找到更优雅的解决方案 .
1 回答
要使按钮更改显示图像的颜色以匹配色调颜色,则需要将按钮作为系统类型启动