ios - 以编程方式添加的自定义 UIBarButtonItem 不尊重 tintColor
问题描述
简而言之,如何更改黑色按钮项目的颜色(同时保持对其大小的控制)?
更长的版本:我以编程方式将一些自定义 UIBarButtonItems 添加到 UIToolbar。我正在使用此处找到的解决方案,以便我可以控制项目的大小。
虽然这解决了 UIBarButtonItem 大小问题,但它不像典型的 UIBarButtonItem 那样尊重 tintColor。所以我得到这样的东西:
大的白色是我想要的颜色,但不是尺寸。此项只是简单地添加到 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))
}
}
一种解决方法是提供白色图像资产,但如果存在,我更愿意找到更优雅的解决方案。
解决方案
要使按钮更改呈现图像的颜色以匹配色调颜色,那么您需要将按钮作为系统类型启动
let button = UIButton(type: .system)
推荐阅读
- python - 杀死给定进程的次要任务
- excel - 错误 1004 “应用程序定义或对象定义错误”
- java - 如何让时间线不再粘在一起?
- git - 如何在 git bash vim 编辑器本身上打开特定文件以在错误消息“e325-attention-swap-file-already-present-error-in-vi”时编辑内容
- javascript - 正则表达式中的多个条件 - 如何用破折号替换冒号和空格
- javascript - JavaScript 中的所有事件侦听器如何保持活动或活动状态?
- ios - 如何仅更改字典键?无需快速更改值
- compiler-errors - 为什么在编译时出现“无法将 't₂' 与 'f32' 类型统一”,我该如何解决?
- php - 自定义简码拉入自定义帖子类型/类别
- linux - linux在行号之后匹配模式直到结束并替换一次