I want to reuse an iPhone's UITableView on the iPad by adding a left/right margin thereby centering it. I tried using the .contentInset
property, but it didn't seem to affect the right margin. Am I missing something? (I tried a negative right-inset right that didn't work either.)
(Here is a gist with test code that you can throw into a UITableViewController subclass.)
// In a UITableViewController subclass
- (void)viewDidLoad {
[super viewDidLoad];
// the space I want on either side of the table
CGFloat xMargin = 100.0f;
// increase content inset and decrease content size an equal amount
UIEdgeInsets contentInset = self.tableView.contentInset;
contentInset.left = xMargin;
contentInset.right = xMargin;
CGSize contentSize = self.tableView.contentSize;
contentSize.width = contentSize.width -= xMargin * 2;
self.tableView.contentSize = contentSize;
self.tableView.contentInset = contentInset;
// Turning autolayout off doesn't appear to help
//self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
}
I can't just shorten the width of the table or the right margin won't have the right color or scroll the table in response to user touches.