let button = UIButton()
button.setImage(UIImage(named: "coin_icon"), forState: UIControlState.Normal)
button.addTarget(self, action:#selector(Profile.goCoin), forControlEvents: UIControlEvents.TouchDragInside)
button.frame=CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
My code is this to add a bar button on navigation bar.
However, i need to change the title of button in a function
func changetitle() {
self.navigationItem.rightBarButtonItem?.title = "Change"
}
I tried this one but didn't work.
How can i change the title of this button?
You need to access the UIButton
from the UIBarButtonItem
and change the title of the UIButton
.
func changetitle() {
let item = self.navigationItem.rightBarButtonItem!
let button = item.customView as! UIButton
button.setTitle("Change", for: .normal)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With