Using UINavigationItem on UITabBarController - swift

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.

Related

Adding a UIBarButtonItem programmatically

I'm trying to add a UIBarButtonItem programmatically.
let navigation = UINavigationController()
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
navigation.navigationItem.rightBarButtonItem = rightBarButton
And also made a selector function for testing.
#objc func logInPressed() {
print("go to login")
}
Unfortunately that does not work - bar button is not visible on navigation bar in simulator.
Checked with a breakpoint, rightBarButtonItem exists.
Probably issue can be caused by creating bar button from app coordinator, not from child VC.
Could please anyone help to troubleshoot this issue?
Simulator screenshot
you are adding UIBarButtonItem to a new instance of NavigationController. so it will not appear there.
so in your view controller which you want to handle right navigation bar , under one of these methods:
override func viewDidLoad()
or
override func viewWillAppear
add:
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
self.navigationController?.navigationItem.setRightBarButton(rightBarButton, animated: true)
Issue was caused by creating bar button not from child VC, but coordinator.
Bar buttons are stored in navigationItem property of UIViewController.

How can I fix my navigation bar back button?

Hi I've been trying to change my back button in my application's navigation bar. The problem is that I see the change when I load the view twice.
I've searched for the answer several times but I don't see what I really want. In fact, I'm new at this language so it's difficult.
What I tried is putting the following lines in the viewWillAppear method and others*:
nav?.navigationBar.backItem?.title = "Messages"
The result is that when I enter the view I see the back button's title as Back. Then if I press on that button and enter the view again the title changes as I want. On the other hand, what I want is to change the title when I load the view first.
*-> I've tried the same line in viewDidLoad too see if that does anything and in viewWillDisappear of the previous view, but nothing happens.
You are using method a change back button in your current ViewController it is wrong, because your navigationBar configured in past controller. If you use Storyboard, please will try this method in your parent controller:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let backItem = UIBarButtonItem()
backItem.title = "Messages"
navigationItem.backBarButtonItem = backItem
}
If you init newController in your parentController, you must to modify your a backButton in parentController, example:
// It's action for any button witch present your newController
#objc private func presentDetailViewContorller() {
let weatherDetailController = WeatherDetailController()
let navController = NavigationController(rootViewController: weatherDetailController)
let backItem = UIBarButtonItem()
backItem.title = "Messages"
navController.backBarButtonItem = backItem
self.present(navController, animated: true, completion: nil)
}
Good luck!

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

Perform segue and don't show "Back" button on the destination view controller

I would like to seque programmatically to a "setup-VC" when the initial setup is not done. This works, BUT in this case I don't want to show the back button on the "setup-VC".
What I've done till now:
I've created two VC. (main-VC and setup-VC)
The code of main-VC:
...
if InitialSetupIsDone == true {
println("Loading the data...") //PPP
} else {
println("Segue to setup screen...") //PPP
self.performSegueWithIdentifier("segueToSetup", sender: self)
}
...
// Pull any data from the view controller which initiated the unwind segue.
#IBAction func segueToSetup(sender: UIStoryboardSegue)
{
let sourceViewController: AnyObject = sender.sourceViewController
}
In the main-VC I've created an unwind-segue ("EXIT"-action? - red icon at the top of the VC) and named it "segueToSetup".
At last point I've connected the "setup-VC" with the "main-VC" while dragging an segue from main-VC (yellow Icon top left) to the setup VC.
Then it would look like this:
(The upper connection goes directly from setup sign to the "setup-VC")
The goal for me now would be to kind of "hide" the back button in "setup-VC" while the initial setup is not done. Maybe I've don't create the unwind segue right... !? Thx
Write this line in destination VC viewDidLoad
navigationController?.navigationItem.setHidesBackButton(true, animated: true)
In viewWillAppaer method also write this
navigationController?.navigationItem.setHidesBackButton(true, animated: true)
This disabled the button for me:
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton