Hiding a UINavigationBar after hitting back button - ios

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

Related

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.

iOS Simple View Pop from Navigation Stack Error

My storyboard looks like the following:
What I'm trying to achieve is when "Click Me" is pressed on the home page, to segue to "One" , check some logic on this controller, if successful, automatically segue to "Two".
Then when the "Back" button is pressed on "Two" it would take the user back home, essentially popping "One" off the stack.
Below is what my "One" controller looks like:
#implementation OneViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// example logic, in this case just force them to view two
if(1 == 1)
{
[self.navigationController popViewControllerAnimated:NO];
TwoViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:vc animated:YES];
}
}
#end
I'm getting strange behavior and receiving the following error:
Finishing up a navigation transition in an unexpected state.
Navigation Bar subview tree might get corrupted.
I can't seem to figure out what I'm doing wrong. I've included the full dead simple source: http://andrewherrick.com/spike/pushpop.zip
EDIT:
I've tried moving the logic to ViewDidAppear and it simply kicks me back to the "Home" view automatically which isn't what I want.
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated {
// example logic, in this case just force them to view two
if(1 == 1)
{
[self.navigationController popViewControllerAnimated:NO];
TwoViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:vc animated:NO];
}
}
Putting your second push logic in viewDidAppear: rather than viewDidLoad solved the problem for me.
You should also consider the UX of your app. If a viewController is being used only for a few seconds to process some data and automatically segues into another viewController, it would be better practice to show a UIActivityIndicator or a small visual indicator.
EDIT:
Your automatic push should not pop itself before pushing another.
- (void)viewDidAppear:(BOOL)animated
{
if(1 == 1)
{
TwoViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:vc animated:YES];
}
}
Then in your TwoViewController you need to programmatically call
[self.navigationController popToRootViewControllerAnimated:YES];
in order to pop back to your Main viewController.

How to hide navigation bar on a particular view controller inside of navigation controller

I have 8 view controllers embedded into 1 navigation controller. I'd like to hide the navigation bar (keeping status bar) on my first view controller. When I've tried to do this this, the navigation bar disappears on all my view controllers.
Try this in the first view controller:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}

NavigationBar Hidding issue on Back

On View1 I hide the navigationBar in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
Then I navigate to View2 where I show the navigationBar
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO];
self.title = #"Title";
}
But on back to View1 again, the navigationBar doesn't hide, even if I did tried to hide it after the pushViewController in View2
[self.navigationController pushViewController:View1 animated:YES];
[self.navigationController setNavigationBarHidden:YES];
I also tried to hide the navigation from viewWillAppear in View1 and it hides it, but there is an ugly delay and I don't find it as a good practice.
So can anyone help me with this issue, how can I hide correctly the navigationBar on back to View1?
The best practice to do what you want is putting bellow in your first viewController:
- (void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
The ViewController1 is not going to get allocated again and so viewDidLoad is not going to get called.
You can do it in viewWillAppear though. But if you are saying that there is a delay, you can do one more thing.
You can get the reference ofViewController1 in ViewController2. Suppose ViewController1 is the first controller in the navigation controller, then do this:
//ViewController2.m
- (IBAction)backButtonPressed:(id)sender{
ViewController1 *view1 = [self.navigationController.viewControllers objectAtIndex:0];
[view1.navigationController setNavigationBarHidden:YES];
Your code is correct, but you need to write like this:
[self.navigationController setNavigationBarHidden:YES];
first, then write
[self.navigationController pushViewController:View1 animated:YES];
See when you are pushing View2 from View2 in navigation stack than View1 doesn't gets deallocated. it is there in in the stack. So when you popping out View2 that time View1 viewDidLoad won't get called. so your code setNavigationBarHidden to hide navigation bar doesn't executes. So put that code to ViewWillAppear or ViewDidAppear because these methods gets called every time View appears.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}

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

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

Resources