Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@steipete

This comment has been minimized.

Copy link

steipete commented Dec 19, 2018

This Gist is part of the commercial PSPDFKit SDK (https://pspdfkit.com/) 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
You can’t perform that action at this time.