Chief Madog
Chief Madog

Reputation: 1507

How to change navigation header button text in xcode project

I'm adding a button to my nav view bar like this:

let navItem = UINavigationItem(title: "Waiting Room");

let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: nil, action: #selector(addTapped));
navItem.leftBarButtonItem = doneItem
mainNavBar.setItems([navItem], animated: false);

The title of the view bar changes to Waiting room when I do this, but how do I change the text of the button itself that I'm adding from "Done" to something else?

I'm using Swift 4.0

Upvotes: 0

Views: 355

Answers (2)

Yasin Ahmed
Yasin Ahmed

Reputation: 11

Try using this line of code:

buttonName.setTitle("New Title", for: .normal)

in this example, we use the setTitle(_:for:) method of the UIButton class. The first parameter is the new title you want to set, and the second parameter, .normal, specifies the state for which the title should be changed (in this case, the normal state when the button is not highlighted or disabled).

Upvotes: 0

iVarun
iVarun

Reputation: 6611

Try below code:

    let doneItem = UIBarButtonItem(title: "Your title", style: .plain, target: self, action:#selector(addTapped));

    navigationItem.leftBarButtonItem = doneItem

enter image description here

Hope this will help you :)

Upvotes: 3

Related Questions