Swift can't show back button after hiding it - iphone

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.

Related

How can I hide or add a BarButtonItem dynamically when I change the current ViewController?

I want to add a BarButtonItem every time the user jumps to the Controller and remove it dynamically when the user selects another ViewController in the navigation bar.
let filterBtn = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(action(sender:)))
navigationItem.rightBarButtonItem = filterBtn
}
I added two BarButtonItem in my UIStoryBoard and now I want to add one more button dynamically but only if the ViewController is selected like shown above.
It it possible to do it like this? I appreciate any input!
So, navigationItem.rightBarButtonItems is an array and you can remove/append elements. I'll suggest you to try removing "humburger" button item and adding the new one when DocumentationViewController is presented.
navigationItem.rightBarButtonItems?.popLast()
navigationItem.rightBarButtonItems?.append(filterBtn)

Using UINavigationItem on UITabBarController

I have a project with a UITabBarController and a couple of views. Sort of like this:
In this project, the tab bar controller is called by tapping the Tab button on the UIViewController. I would like to get rid of the back button with "Title", and replace it with an "X" icon. When tapped, the "X" icon would dismiss the UITabBarController and return to the caller. I do this all of the time on UINavigationController using a UINavigationItem, but that does not appear to work in this situation. I drag the UINavigationItem to the view and it allows it, but it does not show up on the view and any UIBarButtonItem that I drag and drop on it do not appear.
Is there a way to actually do this? I'd even be ok with leaving the existing back button as it is and just getting rid of "Title"
I figured it out right after posting the question. Just a bit more research is all it took.
To fix this, add the following to the UITabBarController code.
override func viewDidLoad() {
super.viewDidLoad()
let buttonImage = UIImage(named: "back")
let leftButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(dismissTabBar))
leftButton.tintColor = UIColor.black
self.navigationItem.leftBarButtonItem = leftButton
}
#IBAction func dismissTabBar() {
self.navigationController?.popToRootViewController(animated: true)
}
This gives me a black back button with my image. When I tap it, it brings me back to the calling 'UIViewController.

Push Navigation Controller Without Title On Back Button

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)

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]
}