Change Title of a Navigation Bar Button Item

Change title of a navigation bar button item

You need to access the UIButton from the UIBarButtonItem and change the title of the UIButton.

func changetitle() {
let item = self.navigationItem.rightBarButtonItem!
let button = item.customView as! UIButton
button.setTitle("Change", for: .normal)
}

How to change the title of the back bar button item?

Try to override previous screen segue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let backItem = UIBarButtonItem()
backItem.title = "My title"
navigationItem.backBarButtonItem = backItem
}

How to set right navigation bar button title

Try this:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:target action:action];

ios/xcode: change title of bar button item

UIBarButtonItem is not UIButton

Please check the following:

  • option-click on _submitButton, you will see a popover with declaration and type.
  • click on UIBarButtonItem, you will see the documentation.
  • there is no method at all to set the title.
  • look at the line Inherits from: on the top under the big class name.
  • UIBarButtonItem is a subclass of UIBarItem
  • click on UIBarItem
  • There is a title property without further arguments, so it's

    _submitButton.title = @"Sign Up";

Reading the documentation is very important, it's impossible to remember all properties/methods of all classes but it's the first thing you should do if you get an error message like this.

Set image and title for bar button item?

You can create UIButton instance, set an image and a title for it, and then create your UIBarButtonItem with it:

    let button = UIButton(type: .System)
button.setImage(UIImage(named: "YourImage"), forState: .Normal)
button.setTitle("YourTitle", forState: .Normal)
button.sizeToFit()
self.leftBarButton = UIBarButtonItem(customView: button)

To add an action:

    button.addTarget(self, action: #selector(self.someAction), forControlEvents: .TouchUpInside)

where self.someAction is

func someAction() {

}

How do I change the title of the back button on a Navigation Bar

This should be placed in the method that calls the ViewController titled "NewTitle".
Right before the push or popViewController statement.

UIBarButtonItem *newBackButton = 
[[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];

Adjust position of bar button item when using large titles with iOS 11

To solve my own problem, I just added a button as a subview of the navbar and set the right and bottom constraints to the navbar. The button will now move up and down when the navbar changes size. However, this requires the button to be removed in any view controllers that you show segue from this view controller. Thus, I added a tag of 1 to the button and removed it from its superview from the other view controller. This is the easiest way to solve it, and I found it the easiest method.

To setup the right button:

func setupNavBar() {

self.title = "Home"
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.isTranslucent = false

let searchController = UISearchController(searchResultsController: nil)
self.navigationItem.searchController = searchController

let rightButton = UIButton()
rightButton.setTitle("Right Button", for: .normal)
rightButton.setTitleColor(.purple, for: .normal)
rightButton.addTarget(self, action: #selector(rightButtonTapped(_:)), for: .touchUpInside)
navigationController?.navigationBar.addSubview(rightButton)
rightButton.tag = 1
rightButton.frame = CGRect(x: self.view.frame.width, y: 0, width: 120, height: 20)

let targetView = self.navigationController?.navigationBar

let trailingContraint = NSLayoutConstraint(item: rightButton, attribute:
.trailingMargin, relatedBy: .equal, toItem: targetView,
attribute: .trailingMargin, multiplier: 1.0, constant: -16)
let bottomConstraint = NSLayoutConstraint(item: rightButton, attribute: .bottom, relatedBy: .equal,
toItem: targetView, attribute: .bottom, multiplier: 1.0, constant: -6)
rightButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([trailingContraint, bottomConstraint])

}

To remove it from any show segued view controllers:

func removeRightButton(){
guard let subviews = self.navigationController?.navigationBar.subviews else{return}
for view in subviews{
if view.tag != 0{
view.removeFromSuperview()
}
}
}

Both functions are called in the viewWillAppear function

How can I change the back navigation bar button item text?

default behaviour of the left bar button item, is to display the title of the previous view Controller

if you want to change the text on the left bar button item, you need to replace the left (back) button like this

UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = @"Back"; // or whatever text you want
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];

how to Change the name of bar button dynamically

Change title like this in your viewDidLoad

if (isUpdate == true) {
let item = self.navigationItem.rightBarButtonItem
item?.title = "Update"
}


Related Topics



Leave a reply



Submit