Last active
August 29, 2015 14:15
-
-
Save lokielse/b8f59b4ce8393832c308 to your computer and use it in GitHub Desktop.
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
//http://stackoverflow.com/a/24166645 | |
// | |
#import <UIKit/UIKit.h> | |
typedef void (^UITableViewCellAutoLayoutRenderBlock)(void); | |
@interface UITableViewCell (AutoLayoutDynamicHeightCalculation) | |
/** | |
* Grab an instance of the receiving type to use in order to calculate AutoLayout contraint driven dynamic height. The method pulls the cell from a nib file and moves any Interface Builder defined contrainsts to the content view. | |
* | |
* @param name Name of the nib file. | |
* | |
* @return collection view cell for using to calculate content based height | |
*/ | |
+ (instancetype)heightCalculationCellFromNibWithName:(NSString *)name; | |
/** | |
* Returns the height of the receiver after rendering with your model data and applying an AutoLayout pass | |
* | |
* @param block Render the model data to your UI elements in this block | |
* | |
* @return Calculated constraint derived height | |
*/ | |
- (CGFloat)heightAfterAutoLayoutPassAndRenderingWithBlock:(UITableViewCellAutoLayoutRenderBlock)block collectionViewWidth:(CGFloat)width; | |
/** | |
* Directly calls `heightAfterAutoLayoutPassAndRenderingWithBlock:collectionViewWidth` assuming a collection view width spanning the [UIScreen mainScreen] bounds | |
*/ | |
- (CGFloat)heightAfterAutoLayoutPassAndRenderingWithBlock:(UITableViewCellAutoLayoutRenderBlock)block; | |
@end |
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
//http://stackoverflow.com/a/24166645 | |
// | |
#import "UITableViewCell+AutoLayoutDynamicHeightCalculation.h" | |
@implementation UITableViewCell (AutoLayoutDynamicHeightCalculation) | |
#pragma mark Dummy Cell Generator | |
+ (instancetype)heightCalculationCellFromNibWithName:(NSString *)name | |
{ | |
UITableViewCell *heightCalculationCell = [[[NSBundle mainBundle] loadNibNamed:name owner:self options:nil] lastObject]; | |
[heightCalculationCell moveInterfaceBuilderLayoutConstraintsToContentView]; | |
return heightCalculationCell; | |
} | |
#pragma mark Moving Constraints | |
- (void)moveInterfaceBuilderLayoutConstraintsToContentView | |
{ | |
[self.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) { | |
[self removeConstraint:constraint]; | |
id firstItem = constraint.firstItem == self ? self.contentView : constraint.firstItem; | |
id secondItem = constraint.secondItem == self ? self.contentView : constraint.secondItem; | |
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:firstItem | |
attribute:constraint.firstAttribute | |
relatedBy:constraint.relation | |
toItem:secondItem | |
attribute:constraint.secondAttribute | |
multiplier:constraint.multiplier | |
constant:constraint.constant]]; | |
}]; | |
} | |
#pragma mark Height | |
- (CGFloat)heightAfterAutoLayoutPassAndRenderingWithBlock:(UITableViewCellAutoLayoutRenderBlock)block | |
{ | |
return [self heightAfterAutoLayoutPassAndRenderingWithBlock:block | |
collectionViewWidth:CGRectGetWidth([[UIScreen mainScreen] bounds])]; | |
} | |
- (CGFloat)heightAfterAutoLayoutPassAndRenderingWithBlock:(UITableViewCellAutoLayoutRenderBlock)block collectionViewWidth:(CGFloat)width | |
{ | |
NSParameterAssert(block); | |
block(); | |
[self setNeedsUpdateConstraints]; | |
[self updateConstraintsIfNeeded]; | |
self.bounds = CGRectMake(0.0f, 0.0f, width, CGRectGetHeight(self.bounds)); | |
[self setNeedsLayout]; | |
[self layoutIfNeeded]; | |
CGSize calculatedSize = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; | |
return calculatedSize.height; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment