0

I have added one custom bar button in my app.I want to set it's title but i am not able to set its title.I am using below code in swift 3.Please advice the best solution.

let customButton=UIButton(frame: CGRect(x: 0, y: 0, width: 60, height:30))
customButton.backgroundColor=Constant.Color.customBlue
customButton.titleLabel?.text = "Add"
customButton.titleLabel?.textColor=UIColor.white
let barButton = UIBarButtonItem(customView:customButton)
barButton.title="Add"
barButton.tintColor=UIColor.white
self.navigationItem.rightBarButtonItem=barButton
TechChain
  • 8,404
  • 29
  • 103
  • 228

3 Answers3

3

You need to set title as below :

 customButton?.setTitle("Add", for:.normal)
KKRocks
  • 8,222
  • 1
  • 18
  • 84
1

You have to use this:

[customButton setTitle:@"Add" forState:UIControlStateNormal];
Eeshwar
  • 277
  • 3
  • 17
0

Try This code:

Note: This is a better approach to apply barButton to your navigationBar with action.

 navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action:  #selector(addTapped))

 func addTapped() {
    //Do your stuff here. After Add button pressed
    print("Add Button Pressed")
 }

If you still want to use your code.Add the following code to set title to your button as KKRocks answer.

  customButton?.setTitle("Add", for:.normal)
Joe
  • 8,868
  • 8
  • 37
  • 59