user2924482
user2924482

Reputation: 9140

iOS: adding UINavigationBar to UITableView programmatically

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.

Upvotes: 1

Views: 782

Answers (4)

wj2061
wj2061

Reputation: 6885

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

        self.edgesForExtendedLayout=UIRectEdge.None

Upvotes: 0

Cameron E
Cameron E

Reputation: 1869

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);

Upvotes: 0

Dharmesh Siddhpura
Dharmesh Siddhpura

Reputation: 1650

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.

Upvotes: 1

Candost
Candost

Reputation: 1029

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)

Upvotes: 0

Related Questions