Push Navigation Controller Without Title On Back Button - swift

I currently have view controller A and view controller B.
View Controller A pushes vc B onto it's navigationController on a button press:
navigationController?.pushViewController(viewControllerB, animated: true)
VC A has a navigation bar that has a title on it "View Controller A Title"
I want VC B to have a left button, which is a back button, and a title, "View Controller B Title"
Currently, when I push VC B onto the nav controller, the title of VC A comes along with it onto the left back arrow, so it looks like
< View Controller A Title View Controller B Title
How do I get rid of the VC A Title?
I've tried setting left bar button item, back bar button item, both with no success.
The only way I've found to remove that title is to do
navigationController?.navigationBar.items?.forEach({ $0.title = "" }) inside of the viewDidLoad of VC B, but then when I click the back arrow, the title is of course missing from VC A
The only solution I've found for this solution listed above here, is to set the navigationItem.title in the viewDidAppear of VC A, but there's about a 1/2 second delay from the title actually showing up which is not ideal.
Is there a better way to not have this title come along with the navigation left bar item?
Thank you in advance!

In View Controller A, just set the backBarButtonItem.
navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)

Related

How to display the tabs when click the back button from hided tab view controller

I created a tab bar controller and from one tab item I gave segue to the navigation view controller. And I create a some view controllers attached to navigation controller. So in one view controller I don't need a tabs so in that controller I wrote to hide the tab bar controller that is self.tabBarController?.tabBar.isHidden = true.
When I click the back button of navigation controller from hided tab view controller to previous controller, it doesn't show the tab bar items in previous controllers. But I needed tabs in all view controller except in one view controller. Why does it not show the tabs?
This is my story board :
You can try this in the VC that's before the one you hide the tab in
override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
You can use hidesBottomBarWhenPushedin the view controller which you don't need tabs. Fits your situation.
let controller = ViewControllerTwo()
controller.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(controller, animated: true)
A little more explanation:
self.tabBarController?.tabBar.isHidden = true globally changed the self.tabBarController's property hideTabBar across its children controllers stack.

Avoid showing "back" for back button when navigation title is long in Swift

There is a navigation in my project which I want to config it's navigation back item.
First case: When the UINavigation title is long the title of back button is set to "back"
replacing back button title to "back"
Second case: when it is longer this "back" is not shown.
delete back button title and just show back icon
But I want to show just back icon in the first case too.
Swift 3 - Through Storyboard:
To make navigation bar back button have only back arrow and no "Back" text written, follow the steps:
Go to navigation bar of the root view controller(titled "Home" in screenshot below)
Go to its attribute inspector. Set the back button to a space as shown below:
And that's it!!
This is the simulator screenshot:
Hope it helps!
Add this in viewDidLoad of ViewController which is pushing next ViewController (View Controller Linguini Arabbiatta)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
It will show just a back icon in all the View Controllers which are pushed from that View Controller.
Try this code
if let title = self.navigationController?.navigationBar.backItem?.title {
if title.characters.count > 5 {
self.navigationController?.navigationBar.backItem?.title = "Anything Else"
}
}
Here's Objective-C
[self.navigationItem setHidesBackButton:YES];
Here's Swift
self.navigationItem.setHidesBackButton(true, animated:true);
Edit: if you wish to remove the text only, then here's Objective-C
[self.navigationItem.title = #""];
Here's Swift
self.navigationItem.title = ""
Then you'll need to refresh the nav bar
self.navigationController?.navigationBar.setNeedsLayout()
self.navigationController?.navigationBar.setNeedsDisplay()

Left Bar Button Item - show name of previous View Controller

If you have a Navigation Controller with no Bar Button Items, a navigation back button will be shown with the name of the last View Controller.
I want to keep that name, as in I don't want to have to hardcode it. I do know how to add it in but I don't want to have to do that because that leaves more room for bugs.
Is there a way that I can have a left Bar Button Item and for the default one to not go away?
Add this in viewController where you want to have default back button and custom bar button item. You can customise the bar button item.
override func viewDidLoad() {
super.viewDidLoad()
let newBtn = UIBarButtonItem(title: "new", style: .plain, target: self, action: #selector(anotherMethod))
self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItem = newBtn//self.navigationItem.leftBarButtonItems = [newBtn,anotherBtn]
}

Swift - Overwrite navigation bar

When it comes to opening a new View Controller i didnt want it to be presented modally so i pushed it in navigation controller:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("newView")
self.navigationController!.pushViewController(vc, animated: true)
The View with the identifier "newView" is presented correctly and there is also a navigation bar with a back-button provided, which also works properly.
But how to customize this existing navigation bar, add new bar button items etc.. ?
any suggestions?
Thanks and Greetings!
You are on the right track by subclassing from UIViewController the newView. I doubt you could customize the navigation bar on the storyboard.
You have to do it in code.
In viewDidLoad() of newView, you can have these:
var helloButton = UIBarButtonItem(title: "Hello", style: .Plain, target: self, action: "sayHello:")
self.navigationItem.rightBarButtonItem = helloButton
Implement the sayHello method:
func sayHello(sender: UIBarButtonItem) {
}

Swift can't show back button after hiding it

I have a UITableViewController with static cell that has a custom back button. I hide the back button in viewDidLoad with the code:
var cancelBtn = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "closeSettings")
self.navigationItem.hidesBackButton = true
self.navigationItem.leftBarButtonItem = cancelBtn
If I don't have the self.navigationItem.hidesBackButton = true line in place, the back button arrow still appears. With that line in the code, the back button is hidden and the Cancel button appears exactly how I want. The problem is coming when I open another view controller from a cell, the back button is not present. I instantiate the view controller when a cell is clicked, and use the Navigation Controller to push the View Controller. I have attempted to put
self.navigationItem.hidesBackButton = false
in the code right before pushViewController(). When I do that I see the back button appear, but when the new View Controller is pushed on the Navigation Controller, the back button disappears.
I have even attempted putting the hidesBackButton = false line in the target View Controller's viewDidLoad and viewDidAppear, but I was not able to get the back button to appear. Could anyone help steer me in the right direction on what I could be doing wrong?
Did you try setting self.navigationItem.hidesBackButton = false at viewWillDisappear()?
Try this setting
self.navigationItem.hidesBackButton = false
on the destination view controller in the ViewWillAppear section.