How to change navigation header button text in xcode project - swift

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

Try below code:
let doneItem = UIBarButtonItem(title: "Your title", style: .plain, target: self, action:#selector(addTapped));
navigationItem.leftBarButtonItem = doneItem
Hope this will help you :)

Related

How to center UITabButtonItem in the middle of the screen programatically?

I am trying to move the DONE button in the middle.
Initially, it is located on the left side:
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(ViewController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
Do you have an idea about how to move it to the middle of the screen programmatically? I tried to add flexible space but it doesn't work.
You must two create UIBarButtonItem with SystemItem: .flexibleSpace
and add to toolbar items first and last item :
toolBar.setItems([UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), doneButton, UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)], animated: false)

don't see navigation item in application on Xcode project

I have an application that has a navigation bar
I'm adding a cancel button with a listener in my view controller:
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(addTapped))
but when I run my app
I don't see my cancel button on the navigation bar anymore
what am I doing wrong?
your code should work but can you make sure you did the following.
1. Put the code under the view.didLoad{} in your view controller
class YourViewController: UIViewController {
override func viewDidload() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(addTapped))
}
}
2, Make sure that you have no image covered on your screen because it can hide nav bar

Add rightBarButtonItems to NavBar in UISplitViewController (swift 3)

I am trying to set a custom BarButtonItem to my NavigationBar of my DetailsVC in my UISplitViewController with these two lines of code:
let barbuttonitem = UIBarButtonItem(title: "Hi", style: .plain, target: nil, action: nil)
navigationItem.rightBarButtonItems = [barbuttonitem]
but somehow nothing happens. When i create a new project and add these 2 lines of codes for my navBar it works, but in my real App it doesn't.
Thanks for your help

Changing navigationItem.rightBarButton isn't resulting in visual changes

For whatever reason my rightBarButton is 1. not going away when I set the rightBarButton to nil and 2. not changing when I set it to another UIBarButtonItem.
I can successfully change the title on the fly so I don't suspect the inherited navigation controller is the problem.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "Settings"), style: .Plain, target: self, action: nil)
This simply does nothing and the right bar button remains as a button with text "Cancel".
Probably you are have some problem with image: UIImage(named: "Settings"). Check the name of the image or try to use this to fill a UIImageView
EDIT:
this the code that I used to fill a item with an image and act like a button
//set frame
button.frame = CGRectMake(0, 0, 30, 30)
button.addTarget(self, action: #selector(TorchView.showRank(_:)), forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: button)
let editButton = UIBarButtonItem(image: UIImage(named: "question"), style: .Plain, target: self, action: #selector(TorchView.tapQuestion))
//assign button to navigationbar
self.navigationItem.rightBarButtonItems = [editButton, barButton]
In this case I am using 2 rightButtons

How to add a "Continue" button to a top Navigation Bar in iOS/Swift

(Xcode6, iOS8, iPhone, Swift)
I would like to add a "Continue" button on the right of the navigation bar.
How can this be accomplished? I've been trying with some of the methods available on UIBarButtonItem, but can't get it working.
My best effort to date has been:
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStyle, target: self, action: nil)
self.navigationItem.rightBarButtonItem = b
But I'm getting an error on the first line. It doesn't like the "style" parameter. I've also tried
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStylePlain, target: self, action: nil)
But no luck. Still stuck on the style parameter. Any ideas? tyvm Keith :D
EDIT: For posterity, the following line also includes an action setting:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")
Reference:
How to set the action for a UIBarButtonItem in Swift
enum UIBarButtonItemStyle : Int {
case Plain
case Bordered
case Done
}
So you should NOT write 'UIBarButtonItemStyle', but use '.Plain' (note the dot preceding the word):
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)
To add an actual action to the button you use the following (assuming the method sayHello() exists):
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: Selector("sayHello"))
Edit: Typo in code
enumerations in Swift are quite different than ObjC, in a very good way :]
Here you need to use:
UIBarButtonItemStyle.Plain
You can also do:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)
for maximum simplicity!
Side note: Use let instead of var wherever you can, its better for performance, health, etc!