Created
October 7, 2014 16:57
-
-
Save coordinates/2201a62db43adb2269da 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
@interface FlexibleContainerView () | |
@property (nonatomic, weak) NSLayoutConstraint *heightConstraint; | |
@property (nonatomic, unsafe_unretained) CGFloat defaultHeight; | |
@end | |
@implementation FlexibleContainerView | |
- (void)awakeFromNib | |
{ | |
[super awakeFromNib]; | |
// heightConstraint from nib | |
[self.constraints enumerateObjectsWithOptions:NSEnumerationConcurrent | |
usingBlock:^(NSLayoutConstraint *constraint, NSUInteger index, BOOL *stop) { | |
if(NSLayoutAttributeHeight == constraint.firstAttribute) { | |
self.heightConstraint = constraint; | |
self.defaultHeight = constraint.constant; | |
*stop = YES; | |
} | |
}]; | |
// init heightConstraint if undefined | |
if(!self.heightConstraint) { | |
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self | |
attribute:NSLayoutAttributeHeight | |
relatedBy:NSLayoutRelationEqual | |
toItem:nil | |
attribute:NSLayoutAttributeNotAnAttribute | |
multiplier:1.0 | |
constant:0.0]; | |
constraint.priority = UILayoutPriorityDefaultLow; | |
self.heightConstraint = constraint; | |
[self addConstraint:constraint]; | |
} | |
} | |
- (void)setHidden:(BOOL)hidden | |
{ | |
[super setHidden:hidden]; | |
if(self.hidden) { | |
self.heightConstraint.priority = UILayoutPriorityRequired - 1; | |
self.heightConstraint.constant = 0; | |
} | |
else { | |
self.heightConstraint.priority = UILayoutPriorityDefaultLow; | |
if(0 < self.defaultHeight) { | |
self.heightConstraint.constant = self.defaultHeight; | |
} | |
else { | |
CGSize fittingSize = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; | |
self.heightConstraint.constant = ceilf(fittingSize.height); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment