Xchord Xchord - 1 year ago 485
iOS Question

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

I want to center the content of my

that contains
created at storyboard, and
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download