Search code examples
objective-cipadios6xcode4.3

Stopping navigation bar from scrolling with table


I have a UITableViewController which successfully lists in the table view data fetched from a CoreData entity.

I want to have a navigation bar at the top with a couple of buttons to navigate around, but when I add the navigation bar it appears to sit inside the table, bellow any listings, and therefore scrolls wit the table when flicking through the table list.

As it is a UITableViewController I cannot make the table area smaller it seems and so I have no idea how to add the navigation bar outside of the table.

Any advise on how to make the fix the navigation bar in place so that it does not move.

Here is the code use to set the navigation bar:

- (void)viewDidLoad
{

 UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, 1024,44)];
 UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"Observation details"];
 [navBar pushNavigationItem:navItem animated:NO];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButton)];
navItem.leftBarButtonItem = backButton;
UIBarButtonItem *detailsButton = [[UIBarButtonItem alloc] initWithTitle:@"Observation Details" style:UIBarButtonItemStyleBordered target:self action:@selector(detailsButton)];
navItem.rightBarButtonItem = detailsButton;
 [self.view addSubview:navBar];
}

Solution

  • Don't drag the navigation bar and drop on the table view. That way you are setting that view (a navigation bar) as the header of the table view.

    To do what you are trying to do, the easiest way is to use a navigation controller with the table view as its rootTableView.

    myNavController = [[UINavigationController alloc] initWithRootViewController:myVC];
    
    [self presentViewController:myNavController ...
    

    You can also use a navigation bar without a navigation controller. For that, you have to set a View with a navigation controller at the top and a table view filling the rest of the view.