Skip to content

Instantly share code, notes, and snippets.

Created November 16, 2018 07:50
Show Gist options
  • Save douglashill/36d6c57c3428c7a8380840da89aa386f to your computer and use it in GitHub Desktop.
Save douglashill/36d6c57c3428c7a8380840da89aa386f 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:@[
_pspdf_layoutMarginsGuide = layoutGuide;
return _pspdf_layoutMarginsGuide;
- (void)layoutMarginsDidChange {
[super layoutMarginsDidChange];
if (_pspdf_layoutMarginsGuide == nil) {
// If it’s not loaded there’s nothing to do.
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 =;
self.layoutMarginsGuideBottomConstraint.constant = margins.bottom;
Copy link

This Gist is part of the commercial PSPDFKit SDK ( and is separately licensed as MIT, so other people can use it. Please change the prefix to something else, do not use pspdf_ as this is reserved for our SDK, and will clash if you at some later point integrate us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment