I have a navigationcontroller with navigation flows as shown below:
NC -> A -> B
B appears through a push segue.
The navigationbar of A is made transparent using following
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController!.navigationBar.isTranslucent = true
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController!.navigationBar.shadowImage = UIImage()
}
and is translucency is set to false in viewWillDisappear so that B can have the usual navigation bar:
override func viewWillDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.navigationController!.navigationBar.isTranslucent = false
}
The issue is that when Back button is pressed in B to return to A, The navigation bar of B appears momentarily before disappearing. How to solve this issue?
PS: I do not want to add code to overridden methods of B as B might be shared by other navigation controller.
The issue is that when Back button is pressed in B to return to A, The
navigation bar of B appears momentarily before disappearing. How to
solve this issue?
You do not need to toggle anything in your viewWillDisappear method. Just toggle everything in your viewWillAppear method in your every screens.
Is this what you want? If so, I made a sample project on Github just for you, and for other people who are new to iOS in the future.
https://github.com/glennposadas/showhidenavbar-ios
Though it uses my very simple cocoapod, you can just copy everything from my framework and sample project.
Related
Lets's say, I have 3 view controllers: A, B, C, all embedded into a navigation controller. A and B have a navigation bar, C doesn't.
I have a custom interactive transition between B and C. Since I need my navigation bar to disappear on C, I implemented this function of UINavigationControllerDelegate:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController is C {
navigationController.setNavigationBarHidden(true, animated: animated)
}
else {
navigationController.setNavigationBarHidden(false, animated: animated)
}
}
Everything works perfectly in an common scenario, when I only make push-pop transitions.
But when I cancel transition B->C by calling cancel() on my UIPercentDrivenInteractiveTransition, the navigation bar doesn't show up on B. Here I have to call setNavigationBarHidden(false, ...), but I haven't managed to find the correct place to do it.
If I call it in B's viewWillAppear, navigation bar appears, but looks really strange - it contains elements which the C would have if it had navigation bar. If I pop back to the A, it will blink for a moment with expected contents, but immediately after transition the A navigation bar gets replaced by the B navigation bar!
So, it seems like the navigation bars stack is somehow broken after B->C transition cancellation, it appears to be shifted relatively to viewcontrollers like that:
has
-----------------------------------------------
| ViewController | Navigation bar of |
-----------------------------------------------
| A | B |
-----------------------------------------------
| B | C |
-----------------------------------------------
So, my question is what's the correct place to call navigationController.setNavigationBarHidden(false, animated: true) in this case?
Well, I've managed to find an ugly hack to fix it by myself. Maybe someone in this world would find it helpful.
In my custom UIPercentDrivenInteractiveTransition I override cancel function like that:
class CustomTransitionManager: UIPercentDrivenInteractiveTransition {
/// Indicates transition direction. Must be set before each transition.
var forward: Bool = true
/// Current navigation controller used for transition. Must be set before transition starts
var nc: UINavigationController?
/**
* Hack #1 with UINavigationController here
*/
override func cancel() {
super.cancel()
if forward {
self.nc?.setNavigationBarHidden(false, animated: false)
}
self.nc?.setNavigationBarHidden(forward, animated: false)
}
}
In each of the view controllers (A, B, C) I make the following hack:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide and immediately show navigation bar: this will restore it's correct state
self.navigationController?.setNavigationBarHidden(true, animated: false)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
The best solution probably would be a modal fullscreen presentation of C, but in my case I am working with a project already having corrupted navigation hierarchy and I had no time to fix it properly. Basically, that's the reason why I faced this issue.
Consider a storyboard where we have UITabBarController, in it any UIViewController(lets call it VC) embedded in a UINavigationController. We want VC to have a BarButtonItems on its navigation bar. This storyboard is presented by push segue from another storyboard (having another navigation controller).
Everything looks OK in XCode, but navigation bar does not change in VC at the runtime. However when I change presenting this storyboard from push to modal, everything seems to be fine. IMHO it is because of embedding the navigation controller but I do not see any reason why it is not working. Any idea how to fix it legally (presenting by push) and without any pain would be helpful.
Thanks in advance
So I think you will have to employ some code to fix your issue but not much. I built a test project to test this and will attach images along with code.
First if I understand you correctly you have a navigationController push the new storyboard in question. See attached image.
I named the storyboard being pushed because that is what is happening. Then in my storyboard named Push here is the setup.
In the first view controller of the tabbarcontroller I added the below code. Obviously this hides the navigation controller that pushed us here. If you then visit controller number 2 our new navigation controller and items show. If hiding the navigation controller in the tabbarcontroller view controller 1 is not what you want to do then. continue reading.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//or to unhide from returning the opposite ->self.parent?.navigationController?.isNavigationBarHidden = true
self.parent?.navigationController?.isNavigationBarHidden = true
}
If you did not want to hide the navigation controller in the first view controller but when visiting controller 2 you want to see your items then add this to your viewWillAppear and in the first controller in viewWillAppear change the code from true to false.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view, typically from a nib.
self.parent?.navigationController?.isNavigationBarHidden = true
}
This hides the parent navigation controller as basically that was covering up your navigation controller in your example. So above hides the parent navigation controller. This is also why presenting modally worked. Your navigation controller was hidden from the start. Hope this helps.
**Edit
If you want the navigation controller in tab 2 view controller but you want to keep the parent in tab one to be able to go back with the back button you can set this in viewWillAppear instead so it would look like this in view controller 1.
//tabcontroller vc 1
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
And in tabcontroller view controller 2 with the item in the bar you could do this.
//tabbarcontroller vc 2 with own navigationcontroller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.parent?.navigationController?.isNavigationBarHidden = true
}
Finally if you want the back button visible in both controllers but want different right buttons do it programmatically in viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.setRightBarButton(UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(FirstViewController.editSomthing)), animated: true)
}
And if you want to remove it in the other controller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.rightBarButtonItem = nil;
}
In Both of the above examples directly above this, we are keeping the parent navigation controller so you would not need to embed your view controllers of the tab controller inside uinavigation controller.
You could also use a combo of the above code if you want the hide/show parent navigation controller in viewWillAppear as well. Some of this is dependent on the view hierarchy you choose now and in the future.
I thought showing a screenshot would help understand the issue a bit better.
So the context is the following:
I'm in a navigation controller, on the settings screen of the app (which has a navigation item) and when we tap on the back button, we go back to the main screen of the app (for which I've hidden the navigation bar in the viewWillAppear of the main screen because I'm building a custom header view myself).
At soon as I tap on the back button, the navigation bar disappears immediately and I see a black rectangle appears instead until the animation to display the main screen is completed.
Do you know how I can avoid having this black rectangle appear?
Hope the questions makes sense.
Screenshots
Here is the initial settings screen:
When we tape on the back button, this happens... help :D
I know this piece of code is most likely responsible for the error, but I absolutely need to have the navigationBar hidden on the previous screen.
override func viewWillAppear(_ animated: Bool) {
navigationController?.isNavigationBarHidden = true
}
Have you tried the animated method of hiding the navigation bar setNavigationBarHidden(_ hidden: Bool, animated: Bool)?
For Swift3.0
Add below code in First ViewController
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
navigationController?.setNavigationBarHidden(true, animated: animated)
}
Add below code in Second ViewController
func backButtonPressed() {
navigationController?.setNavigationBarHidden(false, animated: false)
navigationController?.popViewController(animated: true)
}
Add below code in Second ViewController
the color can corresponding your custom
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.view.backgroundColor = UIColor.white
}
I have read about similar problems where the problem was caused by having multiple navigation controllers, but I only have one. This is my navigation flow.
VC = UIViewController, NC = UINavigationController
VC1 -modal-> NC -root-> VC2 -show-> VC3
VC1 is not embedded in a navigation controller, I'm starting that modal segue using performSegueWithIdentifier:sender:.
VC2 then is using a show segue to present VC3, which is the one where the back button is not visible. It still works though. But, it does appear if I exit to the home screen and then enters the app again, as shown here:
https://gfycat.com/VelvetyThisHamster.
Any ideas why this is happening?
edit: To make things clear: I want the button both visible and functioning (it's not that it's working that's the problem, but that it's hidden)
EDIT 2:
If I change my navigation flow to this
NC -root-> VC2 -show-> VC3
then the back button works as intended. So the question is, how can I add a regular view controller without a navigation controller before the first navigation controller? I want it before because VC1 should not have a navigation bar and VC2 should be presented modally.
try this
Hidden
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(true, animated: false)
//else use this
self.navigationItem.leftBarButtonItem = nil
}
Show
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(false, animated: false)
//else
self.navigationController.navigationItem.backBarButtonItem.enabled = TRUE
}
Update
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
//use this
self.navigationItem.setHidesBackButton(false, animated: false)
//else
let backButton = UIBarButtonItem(title: "leftbutton", style: UIBarButtonItemStyle.Plain, target: self, action: "buttonMethod")
self.navigationItem.leftBarButtonItem = backButton
}
func buttonMethod() {
print("Perform action")
}
I think I found the source of the problem, so I'll post it here in case someone else runs into the same problem.
The modal presentation between VC1 and NC was made from a background queue (by calling performSegueWithIdentifier:sender: in the completion handler of an NSURLSessionDataTask to be precise). By dispatching that line of code to the main queue the problem seems to disappear.
Turn out, I had NavigationBar tint color set to "Clear". Once I changed it, the back button appeared.
In my App, I have to redirect users to my rootview from my login screen.
I used popViewControllerAnimated it just pops only one view.
So i am using popToRootViewControllerAnimated. But when i am doing it the Navigation bar hides.
I referred some links like this & implemented it. But it is of no use.
Tried Code
override func viewDidAppear(animated: Bool)
{
self.navigationController?.navigationBarHidden = false
}
Thanks in advance!