Skip to content

ShingoFukuyama/UIView-Constraint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

UIView+Constraint

Examples

Centering

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    [v1 addConstraintWidth:100.0 height:100.0];
    [v1 addConstraintCenteringXY];

Locate to bottom like toolbar

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    [v1 addConstraintHeight:44.0];
    [v1 addConstraintFromBottom:0];
    [v1 addConstraintFromLeft:0 toRight:0];

Offset from center

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    [v1 addConstraintWidth:100 height:100];
    [v1 addConstraintCenteringXYWithOffset:UIOffsetMake(-50.0, -50.0)];

From another view

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    UIView *v2 = [[UIView alloc] initWithFrame:CGRectZero];
    v2.backgroundColor = [UIColor greenColor];
    [self.view addSubview:v2];
    
    [v2 addConstraintWidth:100 height:100];
    [v2 addConstraintFromTop:0];
    [v2 addConstraintFromRight:0];
    
    [v1 addConstraintWidth:100 height:100];
    [v1 addConstraintFromViewBottom:v2 distance:50.0];
    [v1 addConstraintFromViewLeft:v2 distance:50.0];

Fill

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    [v1 addConstraintFill];

Fill with inset

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    [v1 addConstraintFillWithInsets:UIEdgeInsetsMake(30.0, 10.0, 100.0, 5.0)];

Constraint with animation

    UIView *v1 = [[UIView alloc] initWithFrame:CGRectZero];
    v1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:v1];
    
    NSArray *constraints = [v1 addConstraintFill];
    // Return an array of NSLayoutConstraint @[top, left, bottom, right]
    
    [self.view layoutIfNeeded];
    [UIView animateWithDuration:2.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        ((NSLayoutConstraint *)constraints[0]).constant = 500.0;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];

About

Easier to manipulate NSLayoutConstraint for UIView

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published