For a client project I recently had to add a subview to a UITableViewCell's contentView and have it centered both horizontally and vertically. This cannot be achieved by simply setting something like the following when configuring the cell in cellForRowAtIndexPath:
myAwesomeSubView.center = cell.contentView.center;
The above will only center the subview horizontally. To achieve vertical centering however, you can instead subclass UITableViewCell and override its method for laying out subviews:
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIView *view in self.contentView.subviews)
{
view.center = self.contentView.center;
}
}
I've had to do this a couple of times and keep forgetting how, so I thought I'd commit it to Google for posterity and anyone else who might need to do the same.