Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change title of a navigation bar button item

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?

like image 240
Utku Dalmaz Avatar asked May 20 '16 15:05

Utku Dalmaz


1 Answers

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)
}
like image 69
rmaddy Avatar answered Oct 04 '22 06:10

rmaddy