The release for this article is 1.1.0.

This category is the one we use most often and provides an easy way to add constraints to views.

1. Public attributes

@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins;
@property (nonatomic, strong, readonly(ios) MASViewAttribute * mas_safeAreaLayoutGuide API_AVAILABLE (11.0), tvos (11.0)); @property (nonatomic, strong,readonly(ios) MASViewAttribute * mas_safeAreaLayoutGuideTop API_AVAILABLE (11.0), tvos (11.0)); @property (nonatomic, strong,readonly(ios) MASViewAttribute * mas_safeAreaLayoutGuideBottom API_AVAILABLE (11.0), tvos (11.0)); @property (nonatomic, strong,readonly(ios) MASViewAttribute * mas_safeAreaLayoutGuideLeft API_AVAILABLE (11.0), tvos (11.0)); @property (nonatomic, strong,readonly(ios) MASViewAttribute * mas_safeAreaLayoutGuideRight API_AVAILABLE (11.0), tvos (11.0)); @property (nonatomic, strong,readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr);
Copy the code

These properties provide a view property wrapper object for the current view’s corresponding constraint property.

@property (nonatomic, strong) id mas_key;
Copy the code

This property holds the key associated with the view.

2. Public methods

*/ - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view;Copy the code
/** Method for adding and installing constraints */ - (NSArray *)mas_makeConstraints: void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;Copy the code
/** Update method for installed constraints */ - (NSArray *)mas_updateConstraints: void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;Copy the code
/** Method for uninstalling constraints and re-adding and installing constraints */ - (NSArray *)mas_remakeConstraints: void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;Copy the code

3. The implementation

- (MASViewAttribute *)mas_left {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeft];
}

- (MASViewAttribute *)mas_top {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTop];
}

- (MASViewAttribute *)mas_right {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRight];
}

- (MASViewAttribute *)mas_bottom {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottom];
}

- (MASViewAttribute *)mas_leading {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeading];
}

- (MASViewAttribute *)mas_trailing {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailing];
}

- (MASViewAttribute *)mas_width {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeWidth];
}

- (MASViewAttribute *)mas_height {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeHeight];
}

- (MASViewAttribute *)mas_centerX {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterX];
}

- (MASViewAttribute *)mas_centerY {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterY];
}

- (MASViewAttribute *)mas_baseline {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBaseline];
}

- (MASViewAttribute *)mas_firstBaseline {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeFirstBaseline];
}
- (MASViewAttribute *)mas_lastBaseline {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLastBaseline];
}

- (MASViewAttribute *)mas_leftMargin {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin];
}

- (MASViewAttribute *)mas_rightMargin {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin];
}

- (MASViewAttribute *)mas_topMargin {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin];
}

- (MASViewAttribute *)mas_bottomMargin {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin];
}

- (MASViewAttribute *)mas_leadingMargin {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin];
}

- (MASViewAttribute *)mas_trailingMargin {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin];
}

- (MASViewAttribute *)mas_centerXWithinMargins {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins];
}

- (MASViewAttribute *)mas_centerYWithinMargins {
    return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins];
}

- (MASViewAttribute *)mas_safeAreaLayoutGuide {
    return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeBottom];
}

- (MASViewAttribute *)mas_safeAreaLayoutGuideTop {
    return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeTop];
}

- (MASViewAttribute *)mas_safeAreaLayoutGuideBottom {
    return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeBottom];
}

- (MASViewAttribute *)mas_safeAreaLayoutGuideLeft {
    return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeLeft];
}

- (MASViewAttribute *)mas_safeAreaLayoutGuideRight {
    return [[MASViewAttribute alloc] initWithView:self item:self.safeAreaLayoutGuide layoutAttribute:NSLayoutAttributeRight];
}

- (MASViewAttribute *(^)(NSLayoutAttribute))mas_attribute
{
    return ^(NSLayoutAttribute attr) {
        return [[MASViewAttribute alloc] initWithView:self layoutAttribute:attr];
    };
}
Copy the code

Each of the above implementations creates a view property wrapper object that takes the current view and the specified constraint property as parameters and returns it to the caller.


- (id)mas_key {
    return objc_getAssociatedObject(self, @selector(mas_key));
}

- (void)setMas_key:(id)key {
    objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
Copy the code

Add a property to the UIView class that holds the key associated with the current view by overwriting the getter and setter for the property and using the association object.


- (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view {// create variable to save public superview MAS_VIEW *closestCommonSuperview = nil; MAS_VIEW *secondViewSuperview = view; // Loop as long as no public superview is found and there is a parent of the incoming viewwhile(! ClosestCommonSuperview && secondViewSuperview) {// Create a variable to save the parent view of the current view MAS_VIEW *firstViewSuperview = self; // Loop as long as no public superview is found and there is a parent of the current viewwhile(! ClosestCommonSuperview &&firstViewSuperView) {// If the parent view of the incoming view is the same as the parent view of the current viewifClosestCommonSuperview = secondViewSuperview; (secondViewSuperview == firstViewSuperview) {closestCommonSuperview = secondViewSuperview; } / / if the current parent view does not meet the conditions, you can get the current parent view's parent view firstViewSuperview = firstViewSuperview. Superview. } / / if the incoming parent view does not meet the conditions, you can get to the parent view of parent view secondViewSuperview = secondViewSuperview. Superview. } // Return to the public superviewreturn closestCommonSuperview;
}
Copy the code

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { self.translatesAutoresizingMaskIntoConstraints =  NO; MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; block(constraintMaker);return [constraintMaker install];
}

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    constraintMaker.updateExisting = YES;
    block(constraintMaker);
    return [constraintMaker install];
}

- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    constraintMaker.removeExisting = YES;
    block(constraintMaker);
    return [constraintMaker install];
}
Copy the code

The implementation of these three methods is roughly the same:

  • Add the current view’stranslatesAutoresizingMaskIntoConstraintsAttribute is set toNO.
  • Take the current view as the parameterMASConstraintMakerObject.
  • The factory object can be set to different property values depending on the method constraint:
    • mas_makeConstraints:Method does not set properties.
    • mas_updateConstraints:Methods willupdateExistingProperty set toYES.
    • mas_remakeConstraints:Methods willremoveExistingProperty set toYES.
  • throughblockThe callback constrains the factory object so that the user can create the constraint.
  • Constraints on installation user creation.
  • Returns an array of installed constraint objects.

4. To summarize

Thanks to a variety of good design and packaging, the implementation of this classification is relatively simple. This classification provides an environment for setting constraints. When using it, you only need to consider that if you set constraints, other operations do not need to be concerned.