Randall
Randall

Reputation: 2123

How to add a navigation bar to a table view?

I want to display a settings table modally, but I'm having trouble getting navigation bar at the top (to show the title and a done button). I can add the bar as a subview to the tableview, but then it scrolls with the tableview and I want to it to stay put at the top.

Upvotes: 0

Views: 1623

Answers (2)

Legolas
Legolas

Reputation: 12345

If you are showing it modally, you can do something like this. Here, I am trying to show EnterScreenController modally, and I add a navigation Bar on top like this.

EnterScreenController *enterScreenController = [[EnterScreenController alloc] initWithNibName:@"EnterScreenController" bundle:[NSBundle mainBundle]];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:enterScreenController];

navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentModalViewController:navController animated:YES];

Upvotes: 0

TommyG
TommyG

Reputation: 4155

The best approach to this problem is to declare a UINavigationController in the previous view and then you get the nav bar automatically without interrupting the tableview behavior in your current view.

Example from the AppDelegate:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewTestViewController alloc] initWithNibName:@"ViewTestViewController" bundle:nil]; 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;

Upvotes: 1

Related Questions