How can I change the Back-Button of the currently displayed ViewController?
Configuring the BackButtonItem effects the BackButton of the next ViewController, instead of the currently shown.
e.g.: I have two ViewControllers VCA and VCB. I navigate from VCA to VCB and want to style the BackButton thats shown on VCB. I want to hide the title and set custom icons for normal and highlighted state.
public class VCB : ViewControllerBase
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
var btn = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
// TODO: set the Icon
NavigationItem.BackBarButtonItem = btn;
}
}
Do I have to implement my own BackButton and set it as LeftBarButton, or can I achieve this somehow with the standard BackButton .
You can try this in your ViewDidLoad method:
UIBarButtonItem backButton = new UIBarButtonItem("< Back",UIBarButtonItemStyle.Plain, null);
this.NavigationItem.SetLeftBarButtonItem(backButton, false);
if the viewController already has a BarButtonItem in the View,you should be able to use this.NavigationItem.BackBarButtonItem and change its properties.
Related
I have multiple view controllers embedded inside a navigation controller. I have an option that changes the language of the user interface. When user chooses French, the user interface should update itself with French language. All elements are updated with French as I expected, except the button that takes user back to the previous view controller (as you can see in the screenshot - "List of Events").
This is the function I call to update UI :
func updateView() {
DispatchQueue.main.async {
// CurrentSelection.LanguageUI holds a reference to currenly selected language object
navigationItem.title = CurrentSelection.languageUI.event_singular
navigationController?.title = CurrentSelection.languageUI.listOfEvents <<- this line doesn't work
}
}
The navigationController?.title holds "List of Events" string and the assignment statement seems to work. It just that the UI isn't updated with the new title value. Where am I doing wrong?
It's the previousVC that decides what it's own back button will be.
So set this on the previousVC in viewDidLoad
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back title" style:UIBarButtonItemStylePlain target:nil action:nil];
If this isn't set then the back button will be the .title of the previous VC.
-
In your situation you should have some kind of language change notification that the previous VC can listen for and know to update its own title.
One way to do it is to implement UINavigationControllerDelegate and set the back bar button item there:
class YourViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
}
// MARK: - Navigation controller delegate
public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
let item = UIBarButtonItem(title: "Custom title", style: .plain, target: nil, action: nil)
viewController.navigationItem.backBarButtonItem = item
}
}
Something to be aware of is that this needs to be implemented in the view controller which pushes the next view controller. So you'll be changing the back bar button item of the view controller that willShow.
Those view controllers in the navigation stack won't change atomatically.
So you have to use Notification center and add observers to the previous view controllers and change the Title of the navigation bar with selected language when it is called.
When language is changed you have to post the change.
Then it will change the title to french so does the back button will
change.
How do I add a button to navigation bar that appears only in a particular view controller? And also how to add different buttons to different view controller that is embedded in navigation controller.
You don't have to add buttons to navigation bar, rather you could add bar button items. If you want you can customise bar button item to meet your requirements like setting a custom image as suggested by #Pushpendra.
Programatically:
Your View Controller is responsible for providing bar button item(s) for it's navigation bar. Create bar button items inside each of your view controller's viewDidLoad() or in init() if you have overridden it.
Storyboards:
It's pretty handy to drag n drop bar button items from storyboard editor and have the outlets setup so that you can change the behaviour later.
If you are precisely looking to control the behaviour, then you can add or remove a bar button as and when needed depending on your conditional requirements.
class FooViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
/// Assuming this view controller presented/pushed with navigation controller
let buttonItem = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(FooViewController.buttonItemAction(sender:)))
self.navigationItem.leftBarButtonItem = buttonItem
}
#IBAction func buttonItemAction(sender: UIBarButtonItem) {
/// some action
}
}
class BarViewController: UIViewController {
var buttonItem: UIBarButtonItem?
var showButtonItem = false
override func viewDidLoad() {
super.viewDidLoad()
/// Assuming this view controller presented/pushed with navigation controller
buttonItem = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: #selector(BarViewController.buttonItemAction(sender:)))
self.navigationItem.leftBarButtonItem = buttonItem
}
#IBAction func buttonItemAction(sender: UIBarButtonItem) {
/// some action
}
#IBAction func toggleButtonItem(sender: UIButton) {
self.navigationItem.rightBarButtonItem = (self.showButtonItem == true) ? self.buttonItem : nil
}
}
If this is what you are not looking for then update your question along with the code that you have tried.
try this :-
extension UIViewController {
func addButton(img:String){
let imgIcon = UIImage(named: img)
let btn = UIBarButtonItem(image: imgIcon, style: .plain, target:
self, action: #selector(self.btnAction))
// if you want to set right button then add right bar button item
self.navigationItem.leftBarButtonItem = btn
}
func btnAction(){
}
func removeButton() {
self.navigationItem.leftBarButtonItem = nil
}
}
when you want to add button
in viewDidLoad :-
self.addButton(img:"imageName")
also if you want to perform separate action on all view controller then
call this on controller
override func btnAction() {
}
to remove :-
self.removeButton()
if you want to add different button on different controller add the button in viewDidLoad and remove in viewWillDisappear
Drag a bar button item in your navigation bar for the specific view controller in your storyboard
The best solution is
Create Viewcontroller inherit from UIViewController or UITableViewController and write a method for setupNavigationButton or buttonText or
(Better to add an action method on a super controller also)
eg:
-(void) setupNavigationButton {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"button1" style:UIBarButtonItemStylePlain target:self action:#selector(buttonActin:)];
}
and call this method on viewDidLoad on the created controller
Next when you create your own controller add this viewcontroler as your parent view controller. So it will automatically add that view.
(You want to write an action on your controller)
Using above method, it saves only one line. But you can save more lines. Let's assume you want to change only text and action on that button.
If it is only you need to change text, write a method to return button text
(Following line for your baseviewcontroller)
-(NSString *) rightButtonText {
return #"";
}
and add following codes for baseviewcontroller
-(void)setNavigationControler {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:[self rightButtonText] style:UIBarButtonItemStylePlain target:self action:#selector(buttonActin:)];
}
and call it on baseviewcontroller
Now your every view controller, you need to override rightButtonText and create selector method and do that action.
Assume you don't need to add that button for some viewcontroler. Then you have a few options
1) Remove baseviewcotroler inheritance from your viewcontroller
2) After call supper viewDidload add following code
self.navigationItem.rightBarButtonItem =
3) Override setNavigationControler method on your controller with the blank method body.
-(void)setNavigationControler {}
In my root view controller I set an array of bar button items with images and assign them to the right bar button.
When I push the next view controller my navigation bar resets and only displays a back button.
Any way to preserve the navigation bar as it was set on the root view controller so it will display on all pages?
As user1046037 has said you can set the item buttons while you are preparing the segue.
Example:
let helpViewController = HelpViewController(nibName: "HelpViewController", bundle: nil)
let someLeftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: "someAction")
helpViewController.navigationItem.leftBarButtonItem = someLeftButton
helpViewController.navigationItem.leftItemsSupplementBackButton = true
navigationController?.pushViewController(helpViewController, animated: true)
This one is to preserve the left button item and the back one.
helpViewController.navigationItem.leftItemsSupplementBackButton = true
If you are going to use the same button in several Viewcontrollers you can create a BaseViewController setting up the button and his behaviors.
class AHBaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationBar()
// Configure Common status bar if you want too.
}
func configureNavigationBar() {
let someLeftButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.refresh, target: self, action: "someAction")
navigationItem.leftBarButtonItem = someLeftButton
navigationItem.leftItemsSupplementBackButton = true
}
}
Then just inherit it, in the viewControllers that you want to show the button(s).
class HelpViewController: AHBaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
When you segue between view controllers using Push, you're going to get the default "horizontal slide" navigation animation which. Try "present Modally" or "present as popover".
I want to know if its possible to remove the navigation bar back button text from an inherited navigation bar. Presently my navigation bar shows "< ControllerName". I want to simply show the "<" back icon. I would also like to know how to show "< Back", and how to remove it completely.
I understand I could do this by adding a bar button item to the storyboard, however is there an easier way to do it?
Note, this code does not work:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
You better custom back button for this task.
but You also can do it in other ways. Ex: You have ViewController1, and ViewController2 (You push ViewController2 from ViewController1)
ViewController1
public class ViewController1: UIViewController {
override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.title = "viewcontroller1 title"
}
}
ViewController2
class ViewController2: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// get previous view controller and set title to "" or any things You want
if let viewControllers = self.navigationController?.viewControllers {
let previousVC: UIViewController? = viewControllers.count >= 2 ? viewControllers[viewControllers.count - 2] : nil; // get previous view
previousVC?.title = "" // or previousVC?.title = "Back"
}
}
}
I think this will work for you.
self.navigationItem.hidesBackButton = true
Solution suggested by #Prashant will remove the back button from navigation bar.
To remove the title, use following:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
How do I set a title and buttons on the navigation bar of a pushed view controller if I push it onto navigationController with navigationController.pushViewController(controller:animated:completion:), whilst keeping the "Back" button?
Thank you in advance!
For anyone wondering how to do this, just override your pushed UIViewController's navigationItem property directly. My mistake was that I was trying to accomplish this with self.navigationController?.navigationItem
Here is the code, that you should write in order to set the navigation title as well as left button
Code :
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//For Setting Title
self.title = "New Title";
//For setting button in place of back button
let leftItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: "leftButtonClicked");
self.navigationItem.leftBarButtonItem = leftItem;
}
Happy coding ...