How to put UITableView always in center of UIView?

It can be done using UIScrollView's contentOffset property.

  1. Make your tableView's frame sitting in the bounds:

    tableView.frame = self.view.bounds;
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    
  2. Declare a method -layoutTableView:

    - (void)layoutTableView {
        CGSize contentSize = tableView.contentSize;
        CGSize boundsSize = tableView.bounds.size;
        CGFloat yOffset = 0;
        if(contentSize.height < boundsSize.height) {
            yOffset = floorf((boundsSize.height - contentSize.height)/2);
        }
        tableView.contentOffset = CGPointMake(0, yOffset);
    }
    
  3. When you call [tableView reloadData], just call [self layoutTableView] afterwards.


Another solution is to adjust the content inset of the table view since the content offset solution didn't work for me. Heres the basic idea (inserted into a custom UITableView subclass):

- (void)reloadData {
    [super reloadData];
    [self centerTableViewContentsIfNeeded];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self centerTableViewContentsIfNeeded];
}

- (void)centerTableViewContentsIfNeeded {
    CGFloat totalHeight = CGRectGetHeight(self.bounds);
    CGFloat contentHeight = self.contentSize.height;
    //If we have less content than our table frame then we can center
    BOOL contentCanBeCentered = contentHeight < totalHeight;
    if (contentCanBeCentered) {
        self.contentInset = UIEdgeInsetsMake(ceil(totalHeight/2.f - contentHeight/2.f), 0, 0, 0);
    } else {
        self.contentInset = UIEdgeInsetsZero;
    }
}

For the Swift-hearted here's a Playground:

import UIKit
import Foundation
import XCPlayground

class CenteredTable: UITableView {
    override func reloadData() {
        super.reloadData()
        centerTableContentsIfNeeded()
    }

    override func  layoutSubviews() {
        super.layoutSubviews()
        centerTableContentsIfNeeded()
    }

    func centerTableContentsIfNeeded() {
        let totalHeight = CGRectGetHeight(bounds)
        let contentHeight = contentSize.height
        let contentCanBeCentered = contentHeight < totalHeight
        if (contentCanBeCentered) {
            contentInset = UIEdgeInsets(top: ceil(totalHeight/2 - contentHeight/2), left: 0, bottom: 0, right: 0);
        } else {
            contentInset = UIEdgeInsetsZero;
        }
    }
}

class DataSource: NSObject, UITableViewDataSource {
    let items = ["Mr", "Anderson", "Welcome", "Back", "We", "Missed", "You"]
    func registerReusableViewsWithTable(tableView: UITableView) {
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = items[indexPath.row]
        cell.textLabel?.textAlignment = .Center
        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
}

let dataSource = DataSource()
let table = CenteredTable(frame: CGRectMake(0, 0, 300, 800), style: UITableViewStyle.Plain)
table.tableFooterView = UIView(frame: CGRectZero)
let container = UIView(frame: table.frame)
container.addSubview(table)
dataSource.registerReusableViewsWithTable(table)
table.dataSource = dataSource
table.reloadData()
XCPShowView("table", container)
container