Left Bar Button Item - show name of previous View Controller - swift

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

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.

How to set navigationBar back button image to default symbols?

there is one screen that i don't want to show back button symbols.
i create a empty image and change the navigation bar back button image.(code like following)
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "mainicon_gray-13")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "mainicon_gray-13")
navigationItem.backBarButtonItem = UIBarButtonItem(title: "demo", style: .plain, target: nil, action: nil)
screen like following Picture
But all of backBarButtonItem changed, i need to set backBarButtonItem to default symbols "<" when i back to the view.
Is there any way to set navigation bar back button image to default image?
i want the navigationBar like following picture
following is the way that i found without change back button settings to do same event.
use leftBarButtonItem and set popViewController to go back before screen.
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "test", style: .plain, target: self,action: #selector(backViewBtnFnc))
}
#objc func backViewBtnFnc(){
self.navigationController?.popViewController(animated: true)
}

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)

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) {
}