简而言之,如何更改黑色按钮项目的颜色(同时保持对其大小的控制)?
更长的版本:我正在以编程方式将一些自定义的uibarbuttonities添加到uitoolbar。我使用的是
虽然这修复了uibarbuttonitem的大小问题,但它不像典型的uibarbuttonitem那样尊重淡色。所以我得到了这样的东西:
白色的大物件是我想要的颜色,但不是尺寸。该项只是添加到ib中,将tintcolor设置为默认值。
小的黑色项目是我想要的大小,添加了以下代码(注意,用//标记的行不会产生预期结果):。
对于e-in(self.profile)而言,
.表达式)!{
让按钮=uibutton()
button.setimage(uiimage(命名为“emojiph”),用于:.normal)
button.addTarget(self,action:选择器(onemojiinsert),用于:.touchupInside)
让baritem=uibarbuttonitiem(自定义视图:按钮)
baritem.tag=e.family_expression_id
让wconstraint=baritem.customView?.widthanchor.constraint(等于constant:32)
W培训?.isactive=真
让hconstraint=baritem.customView?.heightanchor.constraint(等于constant:32)
H培训?.isactive=真
//不产生预期结果
button.tingcolor=uicolor.white
//不产生预期结果
自定义视图?.tintcolor=uicolor.white
//不产生预期结果
baritem.tintcolor=uicolor.white
self.emojibar.items?.附加(baritem)
//添加柔性垫片
如果(e.family_expression_id<(self.profile).expressions.count)!){
self.emojibar.items?.append(uibarButtonItem(barButtonSystemItem:uibarButtonSystemItem.flexiblespace,target:nil,action:nil))。
}
}
解决方法是提供白色图像资产,但我更希望找到更优雅的解决方案(如果存在)。
the solution found here这样我就可以控制项目的大小。
虽然这修复了uibarbuttonitem的大小问题,但它不像典型的uibarbuttonitem那样尊重淡色。所以我得到了这样的结果:

白色的大物件是我想要的颜色,但不是尺寸。这个项目只是添加到ib中,并将tintcolor设置为默认值。
小的黑色项目是我想要的大小,并添加了以下代码(注意,标有//的行不会产生预期结果):
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))
}
}
解决方法是提供白色图像资产,但如果存在,我更希望找到更优雅的解决方案。