navigationController popToRootViewController, the navigation bar shows backButton and son view's title - navigationcontroller

I'm beginner for iOS dev. In my app, RootView jump to son views: A、B、C、D through buttons (defined in storyboard, push segue). i use some image to be touched so the son views can push to each other, I.E the code in B Controller:
// jump to View C
[self.navigationController pushViewController:CView animated: YES];
the code in C Controller, define backButton clicked to show RootView:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
[self.navigationController popToRootViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}
It's perfect to show RootView, but the top shows backButton and title which i defined in B Controller. RootView have an image title. Other view's title was set in storyboard.
What can i do now?

solved, the navigationBar also need to be poped:
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// add this pop:
[self.navigationController.navigationBar popNavigationItemAnimated:NO];
[self.navigationController popToRootViewControllerAnimated:NO];
}
[super viewWillDisappear:animated];
}

Related

segue adds a navigation to the view

I have added a NavigationBar to View2 with a "Back" item and then I ctrl-dragged from this item to View1 to add a segue (a show segue). Now whenever "Back" is used to navigate to View1, I get a navigation bar (with an item "back") to this view (View1). I only want a navigation bar to View2, not View1. I can always hide View1's NavigationBar programmatically but I am wondering if I am doing something wrong.
You need to hide navigation Bar for View1 inside ViewWillAppear and unhide while going to viewWillDisappear:
View1:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setHidden:true];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setHidden:false];
}
View2:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setHidden:false];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setHidden:true];
}
Any one of class function you can use, either view1 functions or view2 functions to hide and unhide navigation bar while switching controller.

How to remove black space using Navigation Bar

I am using Navigation controller.
In my first screen their is no need of NavigationBar. As this is the Home screen.So I am hiding it using this code:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES];
}
When I push to new screen I am showing the NavigationBar using this code:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController NO];
}
The problem is when I come back from other screens to my HomeScreen I am getting black screen in place of navigationBar.
Here is the problem in Image:
White color screen is my ViewController which has navigation bar and BLue one has'n Navigation bar. How can I remove the black part.
Click on the navigation controller and go to attribute inspector and uncheck the show navigation bar option as shown in the screenshot:
try this one
self.navigationItem.hidesBackButton = YES;
There is a better way to do it. All you need to do is create a subclass of UINavigationController class. Set the UINavigationControllerDelegate. Add the following method in the class.
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if([viewController isKindOfClass: [SomeClass class]])
[self setNavigationBarHidden: NO];
else
[self setNavigationBarHidden: YES];
}
OR
-(void)viewWillDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}

xCode Navigation Back Button not popping detailview

Whenever I press on the 'go back' button on my final detail view, it wont go back to the table view.
I have a storyboard setup like this:
UITabViewController -> UINavigationController -> UITableView -> UINavigationController -> UIView (The detailview).
When I run my program I see the back button, but clicking it does nothing.
Here is the code for DetailViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithTitle:#"Go Back" style:UIBarButtonItemStylePlain target:self action:#selector(gobackBtn)];
[self.navigationItem setLeftBarButtonItem:backbtn];
[self configureView];
}
- (void)gobackBtn
{
[self.navigationController popViewControllerAnimated:YES];
}
As if [self.navigationController popViewControllerAnimated:YES]; does nothing.
I'm out of ideas. Why does the backbutton not 'pop' the detailview?
Your detail view controller is the root view controller of the navigation controller. So 'popping' does nothing. You need instead to dismiss the navigation controller. Technically, you should use a delegate to tell your TableViewController to dismiss it, but I think
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];
from within your goBackButton method of your detail view controller will do the trick.

Push / Pop View Controller With Navigation Bar from View Controller Without Navigation Bar

I'm trying to push a view controller with a visible navigation bar from a view controller with a hidden navigation bar.
I tried all sorts of combinations of [[self navigationController] setNavigationBarHidden:YES animated:NO]; in viewWillAppear, viewDidAppear, viewWillDisappear... etc.
// First View Controller
#implementation FirstViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:NO];
NSLog(#"[%# viewWillAppear]", self);
}
#end
// Second View Controller
#implementation SecondViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:NO animated:NO];
NSLog(#"[%# viewWillAppear]", self);
}
#end
Nothing worked. I also tried custom code to "animate" a push and pop, which works, BUT I lose the edge swipe and view panning. Before I dig deeper, just want to make sure I'm not reinventing the wheel.
The Starbucks app is what I'm trying to mimic.
The root view controller of the app (the dark background view) is full screen and notice how it doesn't have a UINavigationBar. But when you tap on one of the buttons, it pushes on a view controller (the light background view) WITH a UINavigationBar. From there, if you tap the "back" arrow, it view controller pops with the navigation bar. Interactive pop swipe gesture also works.
It is possible without hacking together a solution by yourself. Here is what you do:
Your root viewController:
#implementation ViewController
....
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
#end
And the pushed viewController:
#implementation SecondViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
#end
This will do. It also keeps the interactive transition working ;)
I find it disturbing, however, that this type of functionality is not documented at all by apple. - You can also hide and show toolbars with these 'call-points' (inside viewWillAppear:)
EDIT
I just realized that this is the same code you wrote in your question. Please test it again. I am 100% sure that this works - I used this functionality in one of my apps, too.
Please also note that my code does use animated:animated instead of your animated:NO. This may be the crucial point here :)
I just set up two view controllers to test this back and forth.
#interface VC1 ()
#end
#implementation VC1
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
#end
and a second
#import "ViewControllerTwo.h"
#interface ViewControllerTwo ()
#end
#implementation ViewControllerTwo
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
#end
VC1 is embedded in a navigationController (which is the root controller for the app), with a button that navigates to ViewControllerTwo. I have a push segue from VC1 -> ViewControllerTwo, this method works. When I tap on the button, the view controller is visible on ViewControllerTwo, when I press back, the navigationBar is gone.

Hiding a UINavigationBar after hitting back button

I have 3 view controllers and they are all chained together like so:
vc1 -> vc2 -> vc3
In view controller 1 and 2, I have the navigation bar hidden like so:
elf.navigationController.navigationBarHidden = NO;
The third view controller shows the navigation bar and has a back button on the left. When I hit the back button it goes to view controller 2 but the navigation bar is no longer hidden. How do I hide it again?
write this code in viewWillAppear method in viewController1 and viewController2
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBarHidden = YES;
}
Thanks
Use this simple code in Third view controller
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}

Resources