Created
November 16, 2018 07:50
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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
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.