I have several custom UITableViewCells in my app, mostly defined by nibs. When moving to iOS 8 and Xcode 6 the margins on the left and right are incorrect. These cells are often interspersed in a table view with default cells.
I made a sample project, here is the margin issue I'm talking about:
The only thing I've been able to find that relates to this is the new property layoutMargins
. For UITableViewCells its value seems to change depending on the device the app is running on:
iPhone 6 and below - layoutMargin: {8, 16, 8, 16}
iPhone 6 Plus - layoutMargin: {8, 20, 8, 20}
This seems to line up with the margins I'm seeing on the standard cells. However, the content for my custom cells is inside the cells contentView
, which has the standard UIView layoutMargin
of {8, 8, 8, 8}
. This means any auto layout constraints that are bound to the Container Margin are adding the incorrect spacing.
The only way I've found to fix this is by adding the following in cellForRowAtIndexPath:
cell.contentView.layoutMargins = cell.layoutMargins;
This doesn't seem like a very good solution going forward (especially since I'd need to wrap it in checks for iOS8 to maintain compatibility).
Does anyone have any ideas? I feel like I must be missing something.
You might want to check out the preservesSuperviewLayoutMargins property. It sound like what you are looking for.
Add the following to any cells that need to be consistently spaced with the standard cells:
- (void)layoutSubviews {
[super layoutSubviews];
if ([self.contentView respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
self.contentView.preservesSuperviewLayoutMargins = YES;
}
}
In your UITableViewCell
subclass, override layoutMarginsDidChange
and set the contentView's layoutMargins to match the cell's layoutMargins:
- (void)layoutMarginsDidChange {
contentView.layoutMargins = layoutMargins
}
i found this to be more reliable than setting preservesSuperviewLayoutMargins
to YES
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With