UINavigationController back button custom text?
From this link:
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Custom Title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
As Tyler said in the comments:
don't do this in the visible view controller, but in the view
controller that you'd see if you hit the back button
How to change the UINavigationController back button name?
In viewWillAppear write this
self.navigationItem.title = @"List View";
And in ViewWilldisapper write this
self.navigationItem.title = @"Back";
It works without story boards.
Change UINavigationBar back button title
Do this in the parent view controller not in the child
Swift
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
Objetive-C
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
How to change UINavigationController Title & backButton programming?
Place the code below in perform(segue) or viewWillDissappear depended on how do you do your presentation - via Storyboard segue or manually from code.
let backButton = UIBarButtonItem()
backButton.title = "whatever_you_want"
navigationItem.backBarButtonItem = backButton
And in viewDidLoad of your vc2
simply put
navigationItem.title = "Controller title"
UINavigationBar Hide back Button Text
In the interface builder, you can select the navigation item of the previous controller and change the Back Button
string to what you'd like the back button to appear as. If you want it blank, for example, just put a space.
You can also change it with this line of code:
[self.navigationItem.backBarButtonItem setTitle:@"Title here"];
Or in Swift:
self.navigationItem.backBarButtonItem?.title = ""
How to set back button text in Swift
The back button belongs to the previous view controller, not the one currently presented on screen.
To modify the back button you should update it before pushing, on the view controller that initiated the segue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let backItem = UIBarButtonItem()
backItem.title = "Something Else"
navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}
Swift 3, 4 & 5:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let backItem = UIBarButtonItem()
backItem.title = "Something Else"
navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed
}
OR
// in your viewDidLoad or viewWillAppear
navigationItem.backBarButtonItem = UIBarButtonItem(
title: "Something Else", style: .plain, target: nil, action: nil)
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];
Changing the back button of UINavigaitonBar with MVVM+C
Below is some Playground code that shows a UINavigationController
with a custom back button that is an image.
Note that what it does is hides the system provided back button, then substitutes another button that still performs the "back" action but on a custom UINavigationController
.
There may be a more efficient way to duplicate the functionality of "back" that doesn't involve a custom class and a custom target-action setup, but I couldn't find one quickly so finding that solution can be left as an exercise for the reader.
import UIKit
import SwiftUI
import PlaygroundSupport
NSSetUncaughtExceptionHandler { error in
debugPrint(error)
}
class MyNavController : UINavigationController {
@objc func goBack(sender: Any?) {
self.popViewController(animated: true)
}
}
let navDestination1 = UIViewController()
navDestination1.navigationItem.title = "Destination 1"
let navigationController = MyNavController(rootViewController: navDestination1)
if #available(iOS 13.0, *) {
let appearence = UINavigationBarAppearance()
appearence.configureWithOpaqueBackground()
appearence.backgroundColor = .purple
appearence.shadowColor = nil
appearence.shadowImage = nil
navigationController.navigationBar.standardAppearance = appearence
navigationController.navigationBar.scrollEdgeAppearance = navigationController.navigationBar.standardAppearance
} else {
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.barTintColor = .purple
navigationController.navigationBar.shadowImage = nil
}
let navDestination2 = UITableViewController()
navDestination2.navigationItem.title = "Destination 2"
navDestination2.navigationItem.hidesBackButton = true
navDestination2.navigationItem.leftBarButtonItem = UIBarButtonItem(
image: UIImage(systemName: "multiply.circle.fill"),
style: UIBarButtonItem.Style.done,
target: navigationController,
action: #selector(MyNavController.goBack))
navigationController.pushViewController(navDestination2, animated: true)
navigationController.view.bounds = CGRect(x: 0,y: 0,width: 320,height: 480)
PlaygroundSupport.PlaygroundPage.current.liveView = navigationController
Related Topics
How to Compare Two Nsdate Objects in Objective C
Programmatically Switching Between Tabs Within Swift
Cannot Set Searchbar as Firstresponder
Is a Date in Same Week, Month, Year of Another Date in Swift
iOS Enterprise Ota Distribution Unable to Download Application
How to Set Device (Ui) Orientation Programmatically
How to Play Mp3 Audio from Url in iOS Swift
Missing Cfbundleiconname in Xcode9 iOS11 App Release
Ios7 iPad Landscape Only App, Using Uiimagepickercontroller
Cocoapods Not Installed or Not in Valid State
Wrapping Text in a Uitextview Around a Uiimage Without Coretext
Correct Implementation of Parent/Child Nsmanagedobjectcontext
Screenshot for Avplayer and Video
Ios: Pre Install Ssl Certificate in Keychain - Programmatically