Skip to content

Instantly share code, notes, and snippets.

@buranmert
Created December 15, 2016 14:04
Show Gist options
  • Save buranmert/2ecb72e7dce3fdf4c77e2d1c4f219226 to your computer and use it in GitHub Desktop.
Save buranmert/2ecb72e7dce3fdf4c77e2d1c4f219226 to your computer and use it in GitHub Desktop.
Autolayout helper
- (void)ag_addSubview:(UIView *)subview withInsets:(UIEdgeInsets)insets {
[self addSubview:subview];
[subview setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraintFromSubview:subview forAttribute:NSLayoutAttributeTop withConstant:insets.top];
[self addConstraintFromSubview:subview forAttribute:NSLayoutAttributeBottom withConstant:insets.bottom];
[self addConstraintFromSubview:subview forAttribute:NSLayoutAttributeLeft withConstant:insets.left];
[self addConstraintFromSubview:subview forAttribute:NSLayoutAttributeRight withConstant:insets.right];
}
- (BOOL)addConstraintFromSubview:(UIView *)subview forAttribute:(NSLayoutAttribute)attribute withConstant:(CGFloat)constantValue {
if (constantValue == AGIneffectiveConstantValue) {
return NO;
}
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:self
attribute:attribute
relatedBy:NSLayoutRelationEqual
toItem:subview
attribute:attribute
multiplier:1.f
constant:constantValue];
[self addConstraint:constraint];
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment