更改 UIBarButtonItem 的字体颜色
Changing font color of UIBarButtonItem
我尝试将右侧栏按钮项的字体颜色更改为紫色,但它仍然显示为白色。我咨询过这个question and this 。我该如何解决这个问题?
代码
let sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 34, height: 15))
sortButton.setTitle("SORT", for: .normal)
sortButton.titleLabel?.tintColor = UIColor.myMusicPurple
sortButton.tintColor = UIColor.myMusicPurple
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sortButton)
navigationItem.rightBarButtonItem?.tintColor = UIColor.myMusicPurple
这应该可以解决问题(如果您有纯文本)
let rightBarButtonItem = UIBarButtonItem(title: "Some text", style: .plain, target: self, action: #selector(someAction))
rightBarButtonItem.tintColor = UIColor.myMusicPurple
navigationItem.rightBarButtonItem = rightBarButtonItem
如何使用:
func setTitleColor(UIColor?, for: UIControlState)
文档说它设置了用于指定状态的标题颜色。
sortButton.setTitleColor( .red, for: .normal)
请试试这个
sortButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
很简单,只需为 UIBarButtonItem 创建一个从 Main.stroyboard 到相应的 swift 文件的引用,
@IBOutlet var yourBarBtn: UIBarButtonItem!
之后写下这一行,
yourBarBtn.tintColor = .white //your_color
就是这样!
首先需要设置plain properties 然后就可以这样写了
@IBOutlet weak var btnGenerate: UIBarButtonItem!
btnGenerate.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.colorBlue], for: .normal)
我尝试将右侧栏按钮项的字体颜色更改为紫色,但它仍然显示为白色。我咨询过这个question and this
代码
let sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 34, height: 15))
sortButton.setTitle("SORT", for: .normal)
sortButton.titleLabel?.tintColor = UIColor.myMusicPurple
sortButton.tintColor = UIColor.myMusicPurple
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sortButton)
navigationItem.rightBarButtonItem?.tintColor = UIColor.myMusicPurple
这应该可以解决问题(如果您有纯文本)
let rightBarButtonItem = UIBarButtonItem(title: "Some text", style: .plain, target: self, action: #selector(someAction))
rightBarButtonItem.tintColor = UIColor.myMusicPurple
navigationItem.rightBarButtonItem = rightBarButtonItem
如何使用:
func setTitleColor(UIColor?, for: UIControlState)
文档说它设置了用于指定状态的标题颜色。
sortButton.setTitleColor( .red, for: .normal)
请试试这个
sortButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
很简单,只需为 UIBarButtonItem 创建一个从 Main.stroyboard 到相应的 swift 文件的引用,
@IBOutlet var yourBarBtn: UIBarButtonItem!
之后写下这一行,
yourBarBtn.tintColor = .white //your_color
就是这样!
首先需要设置plain properties 然后就可以这样写了
@IBOutlet weak var btnGenerate: UIBarButtonItem!
btnGenerate.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.colorBlue], for: .normal)