Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the text of a UIBarButtonItem in Swift

I have a BarButton and I have a multi language application so I want to change its name programmatically based the language I have selected.

I drag it from the storyboard to my swift file but I didn't see any function which can change its name. This is my button:

@IBOutlet weak var detailButton: UIBarButtonItem!

Thanks in advance

like image 760
mike vorisis Avatar asked Aug 18 '16 13:08

mike vorisis


2 Answers

Simply you can set

detailButton.title = "Back"  //Whatever you want set here
like image 108
user3182143 Avatar answered Oct 06 '22 07:10

user3182143


If you are adding a button programmatically in swift 3, you can do it this way (in this example I'm adding a button titled "About" and the selector is a function being triggered):

add an outlet:

 @IBOutlet weak var AboutBarButton: UIBarButtonItem!

then in view did load, assign the title and functions

navigationItem.rightBarButtonItem = AboutBarButton
let aboutButton = UIBarButtonItem(title: "About", style: .plain, target: self, action: #selector(AboutBarButtonPressed(_:)))
navigationItem.rightBarButtonItem = aboutButton
like image 39
Zachary Fisher Avatar answered Oct 06 '22 07:10

Zachary Fisher