How to hide Navigation Controller Navigation Bars in code using Xcode 4

Jun 5, 2011 11

There is a bug in version 1.3 of Dapp which is causing Navigation Controllers not to be hidden when exporting to code.  I’ll be correcting this in the next update, but before then, this is a little tutorial on the code you can use to hide them yourself.

Oh, also please note that this is an extension to another tutorial – Updating Navigation Controller in code.  The other tutorial will show you how to update the look of the Navigation Controller (and how to hide it).  Although I’ll be going into a bit more depth on how to use the viewWillAppear method in your code.

If you want to hide your Navigation Controller throughout your whole app, then you will want to be placing this in your AppDelegate.m file.  Dapp generally creates this file with the format <ProjectName>AppDelegate.m. If you scroll down the code, you will see the part where I inserted the code to hide the Navigation Controller. Just copy this part and place it in your equivalent didFinishLaunchingWithOptions method.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	// Override point for customization after app launch
	// Create the window object
	UIWindow *localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
	// Assign the localWindow to the AppDelegate window, then release the local window
	self.window = localWindow;
	[localWindow release];
 
	// Setup the first view controller
	Page1OffViewController *page1OffViewController = [[Page1OffViewController alloc] init];
 
	// Initialise the navigation controller with the first view controller as its root view controller
	navigationController = [[UINavigationController alloc] initWithRootViewController:page1OffViewController];
 
        // This is where we hide the navigation bar! :)
        [navigationController setNavigationBarHidden:YES];
 
	// Navigation controller has copy of view controller, so release our copy
	[page1OffViewController release];
 
	// Add the navigation controller as a subview of our window
	[window addSubview:[navigationController view]];
	[window makeKeyAndVisible];
 
	return YES;
}

 

Ok, that’s just if we want to hide our Navigation Controller throughout our entire app. There are times where we would like to show it, and then alternately hide it. To specifically control this for every page, you will need to have a viewWillAppear method in your View Controllers which is where you can call code to hide your Navigation Controller.

Just copy the following code underneath your ‘#pragma mark UIViewController Delegates’ section in your code for the View Controller that you wish to hide your Navigation Bar for.

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

 

And that’s it. Pretty simple ;) . I’ve added this to the list of known bugs and will be addressing this in the next update so that you can individually set this within Dapp itself.

To your app success!,
Cliff

P.S. Technology is only a small part of your app success. There is the business, sales and marketing that you need to think of too :). Which is why I also provide a bunch of free education on how to succeed on the App Store - http://kerofrog.com :).

11 Responses to “How to hide Navigation Controller Navigation Bars in code using Xcode 4”

  1. velraj says:

    hey hi can anyone suggest me a sample code for creating segmented control in Xcode 4.2..

    • Cliff says:

      One option is to create a segmented control and export it to Xcode. :) … voila, sample code.

      Another one is to check out the UISegmentedControl class reference from Apple. The trick is to look at the titles at the top and visit the methods for what you need to do.

      For example, ‘initializing a segmented control’, you use – initWithItems:.

      And if you click on it, it will show you what you need to pass.

      Or, you could check out this sample code.

      NSArray *myItems = [NSArray arrayWithObjects:@"One", @"Two", @"Three"];
      UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:myItems];
      [segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
      [segmentedControl setFrame:CGRectMake(0, 0, 320, 44)];
      [self.view addSubview:segmentedControl];
      [segmentedControl release];

      - (void)segmentedControlAction:(id)sender {
      // Handle the segmented control being selected! :) .
      NSLog(@”User selected option %d”, segmentedControl.selectedSegmentIndex);
      }

  2. Thomas says:

    Thank you very much! – Sorry for the long comment, gotta be annoying to have it filling inhere, but really appreciate the fast reply :)

  3. Thomas says:

    Thanks for both the app and the guide – helps a bunch!

    - though i’ve run into some amateur trouble:

    When i open my AppDelegate.m, my code setup does not resemble yours.
    Mine looks as follows:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after app launch
    // Create the window object
    UIWindow *localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Assign the localWindow to the AppDelegate window, then release the local window
    self.window = localWindow;
    [localWindow release];

    // Setup the navigation controllers to load into our tab bar controller
    ForsideViewController *forsideViewController = [[ForsideViewController alloc] init];
    UIImage *tabBarImage1 = [[UIImage alloc] init];
    UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@”Nyheder” image:tabBarImage1 tag:1];
    [tabBarImage1 release];
    forsideViewController.tabBarItem = tabBarItem1;
    [tabBarItem1 release];
    UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:forsideViewController];
    [forsideViewController release];

    FeaturedViewController *featuredViewController = [[FeaturedViewController alloc] init];
    UIImage *tabBarImage2 = [[UIImage alloc] init];
    UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@”Aktuelle” image:tabBarImage2 tag:2];
    [tabBarImage2 release];
    featuredViewController.tabBarItem = tabBarItem2;
    [tabBarItem2 release];
    UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:featuredViewController];
    [featuredViewController release];

    KategoriViewController *kategoriViewController = [[KategoriViewController alloc] init];
    UIImage *tabBarImage3 = [[UIImage alloc] init];
    UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTitle:@”Kategorier” image:tabBarImage3 tag:3];
    [tabBarImage3 release];
    kategoriViewController.tabBarItem = tabBarItem3;
    [tabBarItem3 release];
    UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:kategoriViewController];
    [kategoriViewController release];

    ParkeringViewController *parkeringViewController = [[ParkeringViewController alloc] init];
    UINavigationController *navigationController4 = [[UINavigationController alloc] initWithRootViewController:parkeringViewController];
    [parkeringViewController release];

    tabBarController = [[UITabBarController alloc] init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController1, navigationController2, navigationController3, navigationController4, nil];

    [navigationController1 release];
    [navigationController2 release];
    [navigationController3 release];
    [navigationController4 release];

    // Add the tab bar controller as a subview of our window
    [window addSubview:[tabBarController view]];
    [window makeKeyAndVisible];

    return YES;
    }

    - could you in any way assist me how to hide the NavigationBar ? It works for me in a selfwritten project, where i’ve manually initialized the Bar, but it just can’t get it to work out here.

    Anyways, i’m looking forward to the next update!

    Cheers :)

    • Cliff says:

      You’ll need to hide it for each Navigation Controller. Do it right after you alloc the navigation controller, so for example…

      UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:featuredViewController];
      // Hide nav bar here
      [navigationController2 setNavigationBarHidden:YES];
      [featuredViewController release];

      UINavigationController *navigationController3 = [[UINavigationController alloc] initWithRootViewController:kategoriViewController];
      //Hide nav bar here
      [navigationController3 setNavigationBarHidden:YES];
      [kategoriViewController release];

  4. catarino says:

    cool. thx :)

  5. steven says:

    when is the next update?

  6. Brian says:

    Thanks :) Works perfectly!

  7. Cliff says:

    Basically you want to be changing the ‘Navigation Bar Style’ and the ‘Navigation Bar Translucent’ property to do this. Example code of how to do this would be:

    [navigationController.navigationBar setNavigationBarStyle:UIBarStyleBlack];
    [navigationController.navigationBar setTranslucent:YES];

  8. Brian says:

    Thanks for the guide, my question is, how do you make the navigation bar black transparent?

Leave a Reply