Change back button title when pushing a UIViewController on UITableViewController - ios

View hierarchy:
UINavigationController
->UITableViewController(1)
->UITableViewController(2)
->UIViewController(3)
In 1 and 2 I have this code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
self.navigationItem.backBarButtonItem?.title = ""
}
It should override the back button title of the next view controller pushed on the current view controller. It works from 1 -> 2. But it does not work for 2 -> 3. In 3 the back button title has a title, the name of the previous UITableViewController.
Any ideas whats wrong? I am using swift, xcode6.1 and iOS8.1

You could init a new back button with no title. Just put this in the viewDidLoad() of each view controller.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
--
I am going to extend this answer with my experience. So I managed to remove the title of the back button to display just the back arrow. In storyboard you have to select the navigation item that displays the title inside the navigation bar of the previous view controller. There is a property called Back Button. Just enter a space and save. It will remove the back button title.
Update
UIBarButtonItemStyle.Bordered was deprecated in iOS 8.0. Use UIBarButtonItemStyle.Plain instead.

thanks,
if you want using customer back button image, you can use this
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: “backImage”)
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: “ backImage”)
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: “”, style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

Related

In navigation controler how to implement a add button opens a new view controller

I am new to swift , I have a very simple question. I implement a navigation controller with two items at top like this
I did this by adding a navigation controller to the project and then adding this lines of code in to the viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.title = ""
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .add, target: self, action: Selector(("barButtonItemClicked:"))), animated: true)
}
now my question is about how to open a new view controller I mean a new page after clicks on plus (+) button at the navigation bar. I searched a lot but did not find any exact thing relate to this. Appreciate you if possible help me. thank you
Your code should be like this one:
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, style: .plain, target: self, action: #selector(performToVC))
#objc func performToVC() {
performSegue(withIdentifier: "vc", sender: self)
}
Here is code for Push TO OTherView Controller
#objc func PoushTOHistoryVC() {
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "HistoryViewController") as! HistoryViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
}
here is Button for Add
self.navigationItem.setLeftBarButton(UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(PoushTOHistoryVC)), animated: true)
you need to add HistoryViewControlle to Stroyboard ViewController HistoryViewControlle and Don't forget
First you have to create new view controller which you have to open on that plus (+) button at the navigation bar. After creating new view controller you have to press and hold the control key on the keyboard and the click on the (+) button at the navigation bar and drag it to the new view controller and then release the click button image for reference, then it will show you a pop up with options such as show, show details etcimage for reference, select show option.

iOS: Back button label is cut off with custom font

I'm using a custom font for the navigationItem; When I segue to another view controller the back button on the newly presented view controller is cut off on the left side. I have tried setTitlePositionAdjustment(_ adjustment: UIOffset, for barMetrics: UIBarMetrics) on the first view controller, before doing segue but it didn't displace the button:
And
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Show Contents" {
if let viewController = segue.destination as? ContentsTableViewController {
viewController.navigationItem.backBarButtonItem?.setTitlePositionAdjustment(UIOffsetMake(10, 0), for: .default)
}
}
}
Using this code:
override func viewDidLoad() {
super.viewDidLoad()
let backBarButtonItem = UIBarButtonItem(title: "خانه", style: .plain, target: nil, action: nil)
backBarButtonItem.setTitleTextAttributes([.font : UIFont(name: "B Koodak", size: 32)!], for: .normal)
navigationItem.backBarButtonItem = backBarButtonItem
}
I get the following:
I encountered this issue when was using a custom font and I was hiding the navigation bar in the parent view and showing it in child views (in viewWillAppear(_:)). Figuring that something in this action was causing the button label to get drawn too narrow, I tried calling various UIKit redrawing methods on the navigation bar (setNeedsLayout() & setNeedsDisplay()) in the child view controllers' viewDidLoad() methods, but to no avail. I was able to get the label to draw properly by making the font size smaller, as the OP wrote.
I was eventually able to get it to draw properly at the correct size by manually setting the backBarButtonItem property to a new instance of UIBarButtonItem with "Back" as the title (in my parent view controller). This is possibly why it worked in the accepted answer. This seems to be a bug in UIKit, as I wouldn't think that hiding and showing the nav bar would cause this behavior.

How to customize the navigation back symbol and navigation back text?

This is the back icon and back text now:
But if I want my navigation back like this:
I have tried to set the back to my want icon image:
But it useless.
You can hide back button text in many ways.Try this simple approach.
Step1: Goto your mainstoryBoard and click navigationBar.
Step 2: Goto Attributes Inspector under Navigation Item add a BLANK SPACE in Back Button
Step 3: If you want to change backButton text method is pretty much the same.
Update 1: If you want to use an image as a back button check this link
Update 2:
Method 2: Using custom image as a back button.
Paste below code into your detailVC and set image for your back Button.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
title = "Detail VC"
let customButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(backButtonTapped)) //
self.navigationItem.leftBarButtonItem = customButton
}
func backButtonTapped() {
_ = navigationController?.popToRootViewController(animated: true)
}
I am setting back button image in assets catalogue with the 32pixel size.I am not sure about the asset image size.Check with apple doc about the size class.
Output:
Create a new UIBarButton and add it the navigationItem.leftBarButton.
let backButton = UIBarButtonItem(image: UIImage(named:"yourImage"), style: .plain, target: self, action: #selector(yourBackMethod(sender:))
navigationItem.leftBarButtonItem = = backButton
#objc internal func yourBackMethod(sender: AnyObject) {
navigationController.popViewController()
}
Hope this helps.

How do I find out the navigation controller that is part of the current viewController?

I'm having
a problem getting a button to show on a navigation controller. I have researched this and I think it has to do with how navigation works. Pushed and Popped from a stack.
Here is what I have:
I have the initial view as a navigation controller. This controller calls several views but the view that I'm having an issue with is a UICollectionViewController. This is called from a button click on the first viewController connected to the navigationController. I then have a segue setup that goes from the UICollectionViewCell to a new ViewController. So I'm assuming this is a second level NavigationController.
Secondly I have a Back Button Image that is displayed on my navigation instead of the text < Back.
The back button shows fine in the navigation controller on all the views except for the one called from the second level segue.
As I understand from reading other posts on this topic it is because a new navigation Controller is created on the second level segue.
I'm fine with that because everything works fine but I need the button to show on that second navcontroller.
I was trying to programmatically add the button to the navigation controller associated with the current view. I am using this code:
override func viewDidLoad() {
super.viewDidLoad()
var image = UIImage(named: "NavLogo")
var back_image = UIImage(named: "BackButton")
image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
back_image = back_image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
self.navigationController?.navigationItem.leftBarButtonItem = UIBarButtonItem(image: back_image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
}
This code does not seem to add the button to the current navigation controller.
This project is using Swift 3 and Xcode 8
Use
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
instead of
self.navigationController?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.plain, target: nil, action: nil)
The reason for this is explained in this link.

Can't change text of navigation item back button

I want to hide the text of the back button on the navigation bar and so have found past questions such as this: UINavigationBar Hide back Button Text
However I can't change the text at all, either via using the storyboard, or in code.
See screenshot below for attempt at changing it using the storyboard:
Or if I try to do it programatically by adding the following to viewDidLoad of the pushed view controller
self.navigationItem.backBarButtonItem?.title = "stuff"
It has no effect, nor does moving the same line of code to the view controller doing the pushing.
How come it won't change at all regardless of how I'm trying to change it?
How come using the storyboard, the navigation item title can be set, but not the back button text?
If I add the following to the pushed view controller then I can get the text to change:
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted)
But I would like to understand why none of the other ways of trying to change it have any effect
The title of the back button gets automatically set to the title of the view controller that it will go back to.
To do what you want, you'll have to hide the back button and insert your own button with your own image.
Annoying == #YES.
As Brett mentioned above, a new bar button must be created to change the text.
To set the title of the back button, try the following code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Stuff" style:UIBarButtonStylePlain target:nil action:nil];
Or make it in storyboard by adding a bar button item to your navigation bar.
When it comes to segue from tabBarController to a normal navigation controller, it is always easy to get confused in implementing backBarItem.
The trick is about which controller the backBarItem belongs to. If we navigate from controller A to controller B, then the backBarItem, which is the back button appearing on the controller B's navigation bar, actually belongs to controller A. So we just need to find the right controller to edit the backBarItem.
Solution 1. In the controller A, set the backBarButton self.tabBarController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
//M: In controller A
override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
}
Solution 2. We can use a customised leftBarButton in controller B to cover controller A's backButton. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "<", style: .done, target: self, action: #selector(tapBackButton)), then set the action of the leftBarButton to go back to the previous controller.
//M: in Controller B
override func viewDidLoad() {
super.viewDidLoad()
//M: Hide the default back button.
//M: backBarItem will be covered by the leftBarItem anyway, here is to add an extra handling.
navigationItem.hidesBackButton = true
//M: Customize a leftBarButton.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: " < ", style: .done, target: self, action: #selector(tapBackButton))
//M: Customize the color and font size to the leftBarButton
navigationItem.leftBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 25)], for: .normal)
}
//M: Set the action of the leftBarButton to go back.
#objc func tapBackButton(_ sender:Any){
self.navigationController?.popToRootViewController(animated: true)
}

Resources