DavidK
DavidK

Reputation: 319

Setting the text of a BarButtonItem on the NavigationBar?

Id like to remove an icon and add text to BarButtomItem, but im not sure how to do it. here is my currrent code:

let logoutButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: #selector(SettingsViewController.logout))
        self.navigationItem.setRightBarButtonItem(logoutButton, animated: false)  

I thought i could just do UIBarButtonItem(text: "sign out", ... but I guess that doesn't work.

Upvotes: 2

Views: 3995

Answers (2)

Vicente Garcia
Vicente Garcia

Reputation: 6420

This is an old post but in case somebody else's need this, you can actually create a bar button item with just text:

let logoutButton:UIBarButtonItem = UIBarButtonItem(title:"Sign out",
                                   style:UIBarButtonItemStyle.plain,
                                   target:self,
                                   action:#selector(SettingsViewController.logout))
self.navigationItem.setRightBarButtonItem(logoutButton, animated:false)

Upvotes: 0

0ndre_
0ndre_

Reputation: 3631

you can connect your UIBarButtonItem from the UI.

enter image description here

@IBOutlet weak var logoutButton: UIBarButtonItem! 

It should look like this when you connect it.

Setting of that button can look like this

enter image description here

And finally put this line to ViewDidLoad func:

   //Logout button
    logoutButton.title = "Sign out" //This is the button title

:) It should work.

Upvotes: 1

Related Questions