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.

3
  • 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. Commented Sep 11, 2015 at 21:27
  • @rmaddy can you post how to do it? Commented Sep 11, 2015 at 21:33
  • Please read the "View Controller Programming Guide for iOS" in the docs for everything you need to know on subject. Commented Sep 11, 2015 at 21:34

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this implemented in the view controller?
Not in a view controller but while calling a controller. So here instead of "ViewController" it would be your controller name.
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)

6 Comments

I add UINavigationBar to UITableView [self.table addSubview:navBar]; but still is blocking the first tableviewcell.
You shouldn't add it to UITableView. You should add it to your UIViewController
can you post and example?
There is an example in the link in my answer
in your link is showing the same thing I doing "[self.view addSubview:navbar];"
|
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);

Comments

0

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

        self.edgesForExtendedLayout=UIRectEdge.None

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.