UIBarButtonItem 以编程方式改变颜色
UIBarButtonItem programmatically change color
我有这个左 UIBarButtonItem,它的功能就像一个开关,所以我想在它关闭时将其设为灰色,而在它打开时将其设为正常色调,但我不知道如何实现
这是用于分配按钮的代码:
doneHomeworksButton = UIBarButtonItem(image: doneHomeworksButtonImage, style: .plain, target: self, action: #selector(doneHomeworksClicked))
addButton = UIBarButtonItem(image: plusButtonImage, style: .plain, target: self, action: #selector(plusButtonClicked))
self.navigationItem.rightBarButtonItem = addButton
self.navigationItem.leftBarButtonItem = doneHomeworksButton
这是模拟器(我要换左键)
就是这样,我用文字代替图标。您可以使用 fontawesome 或任何其他的 FM 符号。您还可以在单击按钮时更改图像
class ViewController: UIViewController{
var btnTick:UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
btnTick = UIBarButtonItem(title: "2", style: .plain, target: self, action: #selector(didClickedBtn(_ :)))
btnTick?.tintColor = .blue
navigationItem.leftBarButtonItem = btnTick
}
@objc func didClickedBtn(_ sender : UIBarButtonItem){
sender.tintColor = .red
view.layoutIfNeeded()
}
}
我有这个左 UIBarButtonItem,它的功能就像一个开关,所以我想在它关闭时将其设为灰色,而在它打开时将其设为正常色调,但我不知道如何实现
这是用于分配按钮的代码:
doneHomeworksButton = UIBarButtonItem(image: doneHomeworksButtonImage, style: .plain, target: self, action: #selector(doneHomeworksClicked))
addButton = UIBarButtonItem(image: plusButtonImage, style: .plain, target: self, action: #selector(plusButtonClicked))
self.navigationItem.rightBarButtonItem = addButton
self.navigationItem.leftBarButtonItem = doneHomeworksButton
这是模拟器(我要换左键)
就是这样,我用文字代替图标。您可以使用 fontawesome 或任何其他的 FM 符号。您还可以在单击按钮时更改图像
class ViewController: UIViewController{
var btnTick:UIBarButtonItem?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
btnTick = UIBarButtonItem(title: "2", style: .plain, target: self, action: #selector(didClickedBtn(_ :)))
btnTick?.tintColor = .blue
navigationItem.leftBarButtonItem = btnTick
}
@objc func didClickedBtn(_ sender : UIBarButtonItem){
sender.tintColor = .red
view.layoutIfNeeded()
}
}