Last active
August 29, 2015 14:02
-
-
Save nickynick/72cc10137b8114c0c490 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
#import <UIKit/UIKit.h> | |
@interface LayoutWrapperView : UIView | |
@property (nonatomic, readonly) UIView *view; | |
@property (nonatomic, assign) UIEdgeInsets insets; | |
@property (nonatomic, assign) BOOL collapsesIfViewIsNotVisible; | |
- (id)initWithView:(UIView *)view; | |
- (id)initWithView:(UIView *)view insets:(UIEdgeInsets)insets; | |
@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
#import "LayoutWrapperView.h" | |
#import "Masonry.h" | |
static char kViewVisibilityKVOContext; | |
@interface LayoutWrapperView () | |
@property (nonatomic, strong) NSArray *constraints; | |
@end | |
@implementation LayoutWrapperView | |
#pragma mark - Init | |
- (id)initWithFrame:(CGRect)frame { | |
return [self initWithView:nil]; | |
} | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
return [self initWithView:nil]; | |
} | |
- (id)initWithView:(UIView *)view { | |
return [self initWithView:view insets:UIEdgeInsetsZero]; | |
} | |
- (id)initWithView:(UIView *)view insets:(UIEdgeInsets)insets { | |
NSParameterAssert(view != nil); | |
self = [super initWithFrame:CGRectZero]; | |
if (!self) return nil; | |
self.hidden = YES; | |
self.userInteractionEnabled = NO; | |
_view = view; | |
_view.translatesAutoresizingMaskIntoConstraints = NO; | |
[_view addObserver:self forKeyPath:@"alpha" options:0 context:&kViewVisibilityKVOContext]; | |
[_view addObserver:self forKeyPath:@"hidden" options:0 context:&kViewVisibilityKVOContext]; | |
_insets = insets; | |
return self; | |
} | |
#pragma mark - Dealloc | |
- (void)dealloc { | |
[_view removeObserver:self forKeyPath:@"alpha" context:&kViewVisibilityKVOContext]; | |
[_view removeObserver:self forKeyPath:@"hidden" context:&kViewVisibilityKVOContext]; | |
} | |
#pragma mark - Properties | |
- (void)setInsets:(UIEdgeInsets)insets { | |
if (!UIEdgeInsetsEqualToEdgeInsets(_insets, insets)) { | |
_insets = insets; | |
[self setNeedsUpdateConstraints]; | |
} | |
} | |
- (void)setCollapsesIfViewIsNotVisible:(BOOL)collapsesIfViewIsNotVisible { | |
if (_collapsesIfViewIsNotVisible != collapsesIfViewIsNotVisible) { | |
_collapsesIfViewIsNotVisible = collapsesIfViewIsNotVisible; | |
[self setNeedsUpdateConstraints]; | |
} | |
} | |
#pragma mark - KVO | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { | |
if (context != &kViewVisibilityKVOContext) { | |
return [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | |
} | |
if (self.collapsesIfViewIsNotVisible) { | |
[self setNeedsUpdateConstraints]; | |
} | |
} | |
#pragma mark - UIView | |
- (void)updateConstraints { | |
for (MASConstraint *constraint in self.constraints) { | |
[constraint uninstall]; | |
} | |
self.constraints = [self mas_makeConstraints:^(MASConstraintMaker *make) { | |
if ([self shouldCollapse]) { | |
make.center.equalTo(self.view); | |
} else { | |
make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(-self.insets.top, -self.insets.left, -self.insets.bottom, -self.insets.right)); | |
} | |
}]; | |
[super updateConstraints]; | |
} | |
#pragma mark - Private | |
- (BOOL)shouldCollapse { | |
return self.collapsesIfViewIsNotVisible && (self.view.hidden || self.view.alpha <= 0.01); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment