Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationBar not displaying properling when pushViewController

The problem is pretty simple to understand with pictures. I have a UINavigationController that allow the user to switch between to views.

The first view contains a search bar and a table view like so :enter image description here

The second is a basic view where information about the cell are display

When I click on the search bar, the navigation controller gets hidden and the search bar is now at the top.

Now, if I click on a cell, it goes to the second views, but the navigation bar is first hidden like below :

enter image description here

And then, it automatically appears like that :

enter image description here

I have tried a couple of things like show the navigation bar before pushing the next view controller but it is quite ugly.. Does anyone know how to have the show the navigation bar directly on the second view (like in the contact application)?

[UPDATE] : Code

AppDelegate.m (I'm talking about navigationcontroller2)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
    SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    viewController1.managedObjectContext = [self managedObjectContext];
    viewController2.managedObjectContext = [self managedObjectContext];
    viewController1.viewController2 = viewController2;

    UINavigationController *navigationcontroller1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
    [navigationcontroller1.navigationBar setTintColor:[UIColor lightGrayColor]];
    UINavigationController *navigationcontroller2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
    [navigationcontroller2.navigationBar setTintColor:[UIColor lightGrayColor]];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller1, navigationcontroller2, nil];

    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    return YES;
}

FirstView.m

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!noResultsToDisplay) {
        PinDetailsViewController *pinDetailsViewController = [[PinDetailsViewController alloc] initWithNibName:@"PinDetailsViewController" bundle:nil];
        NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
        Pin *pin = (Pin *) managedObject;

        [self.navigationItem setTitle:@"Pins"];
        [self.navigationController pushViewController:pinDetailsViewController animated:YES];
        [pinDetailsViewController updateWithPin:pin];
    }
}

If you need anything else, just ask but I think it's all there.

like image 639
Titouan de Bailleul Avatar asked May 01 '12 12:05

Titouan de Bailleul


2 Answers

Try to use this code in each viewcontroller.

- (void) viewWillAppear:(BOOL)animated
{

    [self.navigationController setNavigationBarHidden:NO animated:animated];

}


- (void) viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];

}
like image 141
Siba Prasad Hota Avatar answered Sep 28 '22 08:09

Siba Prasad Hota


Before you push the new view controller, you should unhide the navigation bar:

[self.navigationController setNavigationBarHidden:NO animated:YES];

like image 33
Fabian Avatar answered Sep 28 '22 08:09

Fabian