UINavigationItem backBarButtonItem issue during screen transition - ios

I have UINavigationController with 2 view controllers vc1 and vc2 (vc1 pushes vc2). I need to set back button title of vc2, so I do it by:
vc1.navigationItem.backBarButtonItem
= UIBarButtonItem(title: title, style: .plain, target: nil, action: nil)
But this back button title is a counter, I get it from Database, so I usually set it after viewWillAppear but before viewDidAppear (it happens during navigation controller transition from vc1 to vc2)
But the back button title is applied only after the transition is completed. As a comparison if I use leftBarButtonItem with custom view, the title is applied before the transition is completed. But I'm not allowed to use leftBarButtonItem
Here is the Video how it looks like (it's possible to notice if you scroll video slowly)
Is it possible somehow to force update back button title before the transition is completed?

Related

Set custom nav back button with multiple navigation controllers

I have a tab bar controller that has 5 tabs, each with a navigation controller embedded in. There are times where I need to display a view controller from another tab.
I am overriding the stock back button text < BACK to just have it say <, which I do using
let backItem = UIBarButtonItem()
backItem.title = ""
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem = backItem
However, when I load a view controller from a different tab bar (with a different navigation controller), I can't figure out how to remove the < BACK from the supplementary navigation controller so I can just display the button as <. Putting the above code in prepareForSegue: works with one nav controller, but not when there is a second one. Thanks a lot, any insight is greatly appreciated!
The back button that you see when a view controller is pushed onto the navigation controller's stack belongs, not to the pushed view controller, but to the previous view controller, the one that was showing before the push.
If that view controller says:
self.navigationItem.backBarButtonItem =
UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
... then when a view controller is pushed on top of it, the back button will show no title.

Placement of backbutton in navigation controller

I'm using a navigation controller, and I want to replace the default back button and text, with a custom. Here is what I do:
navigationItem.backBarButtonItem = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "BackCarretIcon")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "BackCarretIcon")
This is set in prepare for segue, before navigating away from the root view controller.
When the next view controller appears, the image appears to be in the right place, but when the push animation has completed, the back button image moves a bit down. See image of the destination view controller:
So the "BackCarretIcon" and "back" label are not vertical center alligned. How come?? and how can this be fixed.
There is no code in the view controller life cycle methods of the destination view controller, that uses the navigationcontroller or navigation items.
EDIT
I've put a breakpoint in viewWillLayoutSubviews in the destination view controller. It breaks two times, before the destination view controller appears on screen. When i continue execution after the second break, the destination view controller is pushed onto the screen(with the back carret in the right place). When it is fully pushed onto the screen, the back carret jumps a couple of points down, to the wrong position, and viewWillLayoutSubviews breaks again.
I'm not able to break, at the place where the back carret is positioned right...
Try this:
navigationController?.navigationItem.backBarButtonItem?.setTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -2), forBarMetrics: UIBarMetrics.Default)
Back button is property of the second last VC in the stack of VCs of navigation view controller.
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"BackBack" style:UIBarButtonItemStylePlain target:nil action:nil];
Put this in a child VC in a navigation VC & push another VC on top to it. Back button will display "BackBack". So proper way to do this would be to do this for every child VC. Here user will know that last VC was about "BackBack". But try not to do this. Inform user about where back button will take them. Hacking the way back button works might not be best way & might cause some problem.
Hope this helps.

iOS / Xcode - Navigation Item BackBarButtonItem title shows the one of the previous controller

i have 2 View Controllers connected by a segue within a Navigation Controller and I edited the back items titles in Interface Builder.
My problem is, that the BackBarButtonItem of the second controller shows the title of the first (and presenting) controller's BackBarButtonItem, no matter what I set for Back on the second controller's NavigationItem.
The only thing I've done is setting the leftBarButtonItem for the first Controller:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Close", style: UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
Now all NavigationItems of presented Controllers show "Close". Does anyone have an idea, what's going on?
Thanks in advance
You have to configure desired back button item title on the view controller to which back will be performed, e.g. on previous one.

UINavigationItem BackButton shows the same as the UINavigationItem?

In my UINavigationController I push three of UITableViewController. In my second UITableViewController I set in viewDidLoad:
self.navigationItem.title = "controller2"
self.navigationItem.backBarButtonItem?.title = "c2"
Then I do the following. Starting at UITableViewController one push UITableViewController two and push UITableViewController three on the UINavigationController. When I reach UITableViewController three the back button displays controller2 instead of c2.
I did the same setup on the first UITableViewController and when I reach UITableViewController two the back button shows the correct title.
Why does the back button shows the wrong title?
As answered Here -
The title of the back button is either:
The title of the previous view controller
The name of the previous view controller's navigation item back button
If you are setting the back button for the current view controller's navigation item you are
not setting the button that get's displayed in the current view. You
are in fact setting the back button that will be used if you push
another view controller from it.
Or
You could init a new back button with title. Put this in the viewDidLoad() of view controller.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "c2", style: UIBarButtonItemStyle.Bordered, target: nil, action: nil)

ENSideSwiftMenu: how to use same Navigation Bar for all View Controllers

I am using Evgeny Nazarov's Swift Side Menu for my iOS app and was wondering if anyone has used this same library and able to have the same Navigation Bar for every View Controller that is called from the menu.
I put a BarButtonItem that toggles the sliding menu on my Root View Controller. My problem is that only my Root View Controller shows this Toggle Button. But I would like to have that same Navigation Bar with the Toggle button on every View Controller that is accessible when clicking an option on the sliding menu.
Has anyone had an experience achieving this? Thanks!
UPDATE 1
This is what happens when I added a NavigationBar and BarButtonItem to the other ViewControllers that aren't the RootViewController. The new Navigation Bar ends up under the existing Navigation Bar from the Navigation Controller.
The screen on the left is the Root View Controller and the one on the right is the View Controller when I select the second item on the menu.
UPDATE 2: SOLVED
override func viewDidLoad() {
super.viewDidLoad()
let menuItem = UIBarButtonItem(image: UIImage(named: "icon_menu"), style: .Plain, target: self, action: "menuBarButtonItemClicked")
menuItem.tintColor = UIColor.whiteColor()
self.navigationItem.leftBarButtonItem = menuItem
}
func menuBarButtonItemClicked() {
toggleSideMenuView()
}
You need to put a UIBarButtonItem on the navigation bar in every view controller, not in the root view controller.
I too have used the same design for my app and I solved it this way:
View every sect "Show the Attribute Inspector -> Top bar" the voice "Translucent Navigation Bar", then drag the "Navigation Item" and then "Bar Button Item" in the position you want. Of course every button should have its code or references to the ViewController.
Let me know if you have solved.

Resources