//
//  View+AutoLayout.m
//  Category
//
//  Created by em.
//  Copyright © 2017년 em. All rights reserved.
//

#import "UIView+AutoLayout.h"

@implementation UIView(AutoLayout)


- (void)addTopConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeTop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.superview
                                                               attribute:NSLayoutAttributeTop
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
}

- (void)addBottomConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeBottom
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.superview
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
    
}

- (void)addLeadingConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeLeading
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.superview
                                                               attribute:NSLayoutAttributeLeading
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
    
}

- (void)addTrailingConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeTrailing
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.superview
                                                               attribute:NSLayoutAttributeTrailing
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
    
}

- (void)addWidthConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                     attribute:NSLayoutAttributeWidth
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute:0
                                                    multiplier:1.0f
                                                      constant:constant]] ;
    
    
}

- (void)addHeightConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                     attribute:NSLayoutAttributeHeight
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute:0
                                                    multiplier:1.0f
                                                      constant:constant]] ;
    
}

- (void)addCenterHorizontalConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeCenterX
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.superview
                                                               attribute:NSLayoutAttributeCenterX
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
}

- (void)addCenterVerticalConstant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeCenterY
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:self.superview
                                                               attribute:NSLayoutAttributeCenterY
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
}

- (void)addHorizontalSpaceTo:(id)relateView constant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeLeading
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:relateView
                                                               attribute:NSLayoutAttributeTrailing
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
    //[self.leadingAnchor constraintEqualToAnchor:((UIView *)relateView).trailingAnchor constant:constant].active = YES ;
    
}


- (void)addVerticalSpaceTo:(id)relateView constant:(CGFloat)constant {
    
    [self disableTranslatesAutoresizingMaskIntoConstraints] ;
    
    [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                               attribute:NSLayoutAttributeTop
                                                               relatedBy:NSLayoutRelationEqual
                                                                  toItem:relateView
                                                               attribute:NSLayoutAttributeBottom
                                                              multiplier:1.0f
                                                                constant:constant]] ;
    
    
    //[self.topAnchor constraintEqualToAnchor:((UIView *)relateView).bottomAnchor constant:constant].active = YES ;
    
}


- (void)fillHorizontalToSuperview {
    
    [self addLeadingConstant:0.0f] ;
    [self addTrailingConstant:0.0f] ;
    
}

- (void)fillVerticalToSuperview {
    
    [self addTopConstant:0.0f] ;
    [self addBottomConstant:0.0f] ;
    
}

- (void)fillToSuperView {
    
    [self fillHorizontalToSuperview] ;
    [self fillVerticalToSuperview] ;
    
}

- (void)disableTranslatesAutoresizingMaskIntoConstraints {

    [self setTranslatesAutoresizingMaskIntoConstraints:NO] ;
//    if( self.translatesAutoresizingMaskIntoConstraints ) {
//        
//    }
    
}

@end
