Question

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];
}
Was it helpful?

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.

OTHER TIPS

There are several ways to achieve this. I try to describe the one of which I believe it is easiest based on what I guess is your current level of experience.

Create a new empty view with a separate view controller. Add two subviews to that view of the type container view. These views are designed to carry views that have their own view controllers. One of these containers is on top and has the size of the navigation bar and contains the navigation bar etc. For that one you could create a separate view controller. However, I see no reason why that could not be the same view controller. (File's owner in IB) It may well be a separate instance/class. That is up to you.

The remaining view is occupied by the second container view. This one contains a UITableView for which a UITableViewController (-subclass) is the File's Owner.

Unless you are more familiar with the view hierarchy and view controller hierarchy you should not break with the pattern (as given by default sceleton code and tutorials etc.) of having a 1:1 relation between table view controlers and table views. A UITableViewController has a view property as every UIViewController has. But for a UITableViewController that must be a UITableView and nothing else. The standard UITableViewController is not capable of dealing with a UIView as view property that contains a table view and other views as siblings. Plus it is not a good idea of adding more subviews to the table view with respect to its scrolling behaviour etc.

By separating them you can deal with the table and still take examples straigt of tutorial code plus you can have a navigation bar on top that is not influenced by the table at all.

However, I should mention that you could get all of this much easier by using a proper navigation controller. That would bring in your navigation bar for free. You must have good rasons for not doing so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top