Xchord Xchord - 6 months ago 139
iOS Question

How to vertical align (center) the content of UITableView?

I want to center the content of my

UITableView
that contains
headerView
and
footerView
created at storyboard, and
UITableViewCell
s. How can I achieve this?

Here is what I'm trying to implement to solve my problem but this does not work.

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

CGFloat height = self.tableView.frameHeight - self.navigationController.navigationBar.frameHeight - [UIApplication sharedApplication].statusBarFrame.size.height - (self.rowCount * self.rowHeight);
self.tableView.tableHeaderView.frameHeight = height / 2.0;
}


So I subtracted the height of the navigationBar & statusBar and cells' height to the tableView's height to get the height of the empty area.
Now that I get the height if the empty area, I divided it to 2 for the footer and header's view.

Answer

In the viewWillAppear and in the didRotateFromInterfaceOrientation functions :

CGFloat headerHeight = (self.view.frame.size.height - (ROW_HEIGHT * [self.tabelView numberOfRowsInSection:0]))) / 2;

self.tableView.contentInset = UIEdgeInsetsMake(headerHeight, 0, -headerHeight, 0);

This will solve your problem.

Comments