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.
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 :).
One of the common questions we receive is how to update the Navigation Bar, especially as Dapp will create a separate navigation bar for us but will not update the Navigation Controllers Navigation Bar provided by the iPhone SDK.
A common export of the navigation bar might look as follows -
- (void)loadnav { UINavigationBar *nav = [[UINavigationBar alloc] init]; [nav setBarStyle:UIBarStyleBlack]; [nav setTranslucent:YES]; [nav setTintColor:[UIColor redColor]]; UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Done"]; [nav setItems:[NSArray arrayWithObject:navigationItem]]; [navigationItem release]; [nav setFrame:CGRectMake(0, -44, 320, 44)]; [self addSubview:nav]; } |
The navigation bars y co-ordinates are -44, placing it behind the Navigation Controller that the iPhone SDK inserts. We want to take the attributes from this navigation bar and update the navigation controller with them.
Attributes like bar style, translucent and tintColor need to be assigned in our App Delegate, before the Navigation Controller is loaded. This is because after the Nav Controller is loaded, these attributes will become read-only.
So, after exporting a project, open up the AppDelegate file and insert the following code after the navigation controller is initialised.
// Initialise the navigation controller with the first view controller as its root view controller navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; // Add your code here [navigationController.navigationBar setBarStyle:UIBarStyleBlack]; // I have commented out the set translucent attribute, as it has some interesting effects. Try uncommenting it :). // [navigationController.navigationBar setTranslucent:YES]; [navigationController.navigationBar setTintColor:[UIColor redColor]]; // If you wish to hide the navigation controller // Note: We are able to alternatively hide or show the navigation bar by using // [self.navigationController setNavigationBarHidden:YES] in our viewWillAppear method // within the specific UIViewController that we wish to hide/show the navigation bar [navigationController setNavigationBarHidden:YES]; // Navigation controller has copy of view controller, so release our copy [rootViewController release]; |
Luckily, updating the title is much easier. Apple provide a convenience method for this, in any View Controller (usually in the init method) you can simply type -
self.title = @"My Title"; |
The final item that you might want to add to a navigation bar is a bar button item. Again, in the init method of a view controller a sample bar button item you can create is:
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Info" style:UIBarButtonItemStyleBordered target:self action:@selector(infoButtonAction)]; [self.navigationItem setRightBarButtonItem:infoButtonItem]; |
The action section of the bar button item tells us what method is called when the user presses the button, to handle this call we need to create the method within our code.
- (void)infoButtonAction { // Put your own code in here NSLog(@"User pressed the info button"); } |
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 :).
Categories
- Admin (7)
- Dapp iPhone SDK Tutorials (19)
- Issues (8)
- Kerofrog (1)
- News (20)
- Personal (1)
- Screenshots (6)
- Updates (16)
-
Recent Posts
Archives
Tags
actions AR Augmented Reality Basic RSS Reader Bug Reports Dapp Dapp HD Dapp Project Dapp Sale Dapp Update Documentation events FAQ guidelines International Settings iPad Mockups iPhone 4 iPhone Code Generator iPhone programming tutorial iPhone SDK Tutorial iPhone Simulator iPhone UI Design Issues Layar Player SDK live preview Mockups Movement Handles PDF Wireframes Prototypes Requirements Tab Bar Controller UI Actions UI Events UINavigationBar UINavigationController UIScrollView UIScrollView Tutorial UISwitch isOn Setting Change 4.2 UITabBarController UITableView Updates View Xcode 3.2.2 Xcode 3.2.3 Beta Xcode 4Links
