Forked from douglashill/PSPDFTableViewCell+LayoutMarginsGuide.m
Created
January 2, 2019 12:51
-
-
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.
This file contains hidden or 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