1

I'm adding a UINavigationBar to UITableView programmatically but the UINavigationBar is blocking the first tableview cell.

Here is my code:

self.table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
     UINavigationBar *navBar = [[ UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        navBar.topItem.title = @"tableVIew";
        [self.view addSubview:navBar];

My question is how can add the UINavigationBar without blocking any of the tableview cells.

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173
  • Don't add the nav bar to the table view. Why not put the view controller in a navigation controller? Then you got the nav bar properly and you can push and pop other view controllers as needed. – rmaddy Sep 11 '15 at 21:27
  • @rmaddy can you post how to do it? – user2924482 Sep 11 '15 at 21:33
  • Please read the "View Controller Programming Guide for iOS" in the docs for everything you need to know on subject. – rmaddy Sep 11 '15 at 21:34

4 Answers4

1

Lets say tableView is in "ViewController". Now in-order to call view controller use the below code:

ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

This will add a navigation to your view controller. You need not need to use add subview to add a navigation bar.

Dharmesh Siddhpura
  • 1,610
  • 12
  • 22
0

You shouldn't add the UINavigationBar to UITableView. You should add it to UIViewController instead of adding to any other UIView element. You can find a question-answer about it here.

Also in your code, you set your table's frame is wrong. It should be

CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50)
Community
  • 1
  • 1
Candost
  • 1,029
  • 1
  • 12
  • 28
0

Generally, you probably shouldn't construct your layout this way. But if you are intent on doing it this way, you can always set the tableView.contentInset property to add an inset to the top of the tableView to account for the height of the navigationBar.

self.tableView.contentInset = UIEdgeInsetsMake(navBarframe.size.height, 0.0f, 0.0f, 0.0f);
Cameron E
  • 1,839
  • 1
  • 19
  • 20
0

In your tableviewController's viewdidload func ,Just add a line of code . I assume this will work.

        self.edgesForExtendedLayout=UIRectEdge.None
wj2061
  • 6,778
  • 3
  • 36
  • 62