Andrew
Andrew

Reputation: 16051

How do I add a navigationbar to my app programmatically?

I set up my app a while ago using a tutorial for setting up the navigationbar in interface builder, but no longer use interface builder in any of my app and would much like to change this 1 thing which does use interface builder to being coded in. So my question is, I have a navigationbar which works, and which appears on the first view of my app, HomeView. How would I make this happen just as it does now, programmatically?

Upvotes: 0

Views: 2333

Answers (2)

jorik
jorik

Reputation: 655

Another way of adding the navigation bar programmatically, change the application:didFinishLaunchingWithOptions method of your app delegate like:

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    

    RootViewController *rootViewController = [[RootViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] 
                                             initWithRootViewController:rootViewController];

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

}

Upvotes: 1

PengOne
PengOne

Reputation: 48398

In the AppDelegate.m file, add this:

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    RootViewController *rootViewController = [[RootViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] 
                                             initWithRootViewController:rootViewController];

    [window addSubview:[navController view]];
    [self.window makeKeyAndVisible];

}

Be sure to add #import "RootViewController.h" at the top of the file.

Upvotes: 2

Related Questions