Skip to content

Instantly share code, notes, and snippets.

@MaximAlien
Created December 3, 2015 21:40
Show Gist options
  • Save MaximAlien/dd54f7d5fb1e29f67303 to your computer and use it in GitHub Desktop.
Save MaximAlien/dd54f7d5fb1e29f67303 to your computer and use it in GitHub Desktop.
[iOS] Layout constraints to fill view in parent
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.tableView];
[self setupConstraints];
}
return self;
}
- (void)setupConstraints
{
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeWidth
relatedBy:0
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeHeight
relatedBy:0
toItem:self
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.f];
NSLayoutConstraint *leadingConstraint = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.f];
[self addConstraints:@[widthConstraint, heightConstraint, topConstraint, leadingConstraint]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment