Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pingwinator/c0280476cc3f51d2d7b3dfd3d0724c3a to your computer and use it in GitHub Desktop.
Save pingwinator/c0280476cc3f51d2d7b3dfd3d0724c3a to your computer and use it in GitHub Desktop.
Alternative for a table view cell’s content view’s layoutMarginsGuide that works on iOS 10.
@implementation PSPDFTableViewCell // UITableViewCell subclass
/**
On iOS 10, constraints involving a UITableViewCell’s contentView’s layoutMarginsGuide are removed for some
reason before the cell appears, which breaks the layout. This layout guide is a working alternative.
*/
- (UILayoutGuide *)pspdf_layoutMarginsGuide {
if (@available(iOS 11.0, *)) {
return self.contentView.layoutMarginsGuide;
}
// Constraints to layoutMarginsGuide disappear on iOS 10 for some reason.
if (!_pspdf_layoutMarginsGuide) {
let layoutGuide = [[UILayoutGuide alloc] init];
[self.contentView addLayoutGuide:layoutGuide];
self.layoutMarginsGuideLeadingConstraint = [layoutGuide.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor];
self.layoutMarginsGuideTrailingConstraint = [self.contentView.trailingAnchor constraintEqualToAnchor:layoutGuide.trailingAnchor];
self.layoutMarginsGuideTopConstraint = [layoutGuide.topAnchor constraintEqualToAnchor:self.contentView.topAnchor];
self.layoutMarginsGuideBottomConstraint = [self.contentView.bottomAnchor constraintEqualToAnchor:layoutGuide.bottomAnchor];
[NSLayoutConstraint activateConstraints:@[
self.layoutMarginsGuideLeadingConstraint,
self.layoutMarginsGuideTrailingConstraint,
self.layoutMarginsGuideTopConstraint,
self.layoutMarginsGuideBottomConstraint,
]];
_pspdf_layoutMarginsGuide = layoutGuide;
}
return _pspdf_layoutMarginsGuide;
}
- (void)layoutMarginsDidChange {
[super layoutMarginsDidChange];
if (_pspdf_layoutMarginsGuide == nil) {
// If it’s not loaded there’s nothing to do.
return;
}
bet isRtL = self.pspdf_isRightToLeftLayoutDirection;
let margins = self.layoutMargins;
self.layoutMarginsGuideLeadingConstraint.constant = isRtL ? margins.right : margins.left;
self.layoutMarginsGuideTrailingConstraint.constant = isRtL ? margins.left : margins.right;
self.layoutMarginsGuideTopConstraint.constant = margins.top;
self.layoutMarginsGuideBottomConstraint.constant = margins.bottom;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment