天天看点

Masonrymake:

translatesAutoresizingMaskIntoConstraints

当您通过添加自己的约束条件,使用自动布局来定位视图时,必须将该属性设置为NO。

默认为YES,IB会自动给你设置为NO,代码的话需要代码修改.

make:

(MASConstraint *)left {

  • (MASConstraint *)top {
  • (MASConstraint *)right {
  • (MASConstraint *)bottom {
  • (MASConstraint *)leading {
  • (MASConstraint *)trailing {
  • (MASConstraint *)width {
  • (MASConstraint *)height {
  • (MASConstraint *)centerX {
  • (MASConstraint *)centerY {
  • (MASConstraint *)baseline {

====================================

- (MASConstraint *)edges {

  • (MASConstraint *)size {
  • (MASConstraint *)center {

MASViewAttribute NSLayoutAttribute

view.mas_left NSLayoutAttributeLeft

view.mas_right NSLayoutAttributeRight

view.mas_top NSLayoutAttributeTop

view.mas_bottom NSLayoutAttributeBottom

view.mas_leading NSLayoutAttributeLeading

view.mas_trailing NSLayoutAttributeTrailing

view.mas_width NSLayoutAttributeWidth

view.mas_height NSLayoutAttributeHeight

view.mas_centerX NSLayoutAttributeCenterX

view.mas_centerY NSLayoutAttributeCenterY

view.mas_baseline NSLayoutAttributeBaseline

参数类型

make.right.mas_equalTo(superView.mas_right). offset(padding.right);

make.edges.mas_equalTo(superView). insets(edgePadding);

make.size.equalTo(superview). sizeOffset(CGSizeMake(100, -50))

make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

修改constraint

(1)

@property (nonatomic, strong) MASConstraint *topConstraint;

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {

self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top);

make.left.equalTo(superview.mas_left).with.offset(padding.left);

}];

❗️[self.topConstraint uninstall];

(2)

// this is Apple’s recommended place for adding/updating constraints

// this method can get called multiple times in response to setNeedsUpdateConstraints

// which can be called by UIKit internally or in your code if you need to trigger an update to your constraints

- (void)updateConstraints {

[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {

make.center.equalTo(self);

make.width.equalTo(@(self.buttonSize.width)).priorityLow();

make.height.equalTo(@(self.buttonSize.height)).priorityLow();

make.width.lessThanOrEqualTo(self);

make.height.lessThanOrEqualTo(self);

}];

//according to apple super should be called at end of method
[super updateConstraints];
           

}

实例

//
//  SecondView.m
//  News
//

#import "SecondView.h"
#import <Masonry.h>
@interface SecondView ()
@property (nonatomic,strong)UIView  *testView;
@end

@implementation SecondView
- (id)init {
    self = [super init];
    if (!self) return nil;
//    [self systemMethod];
//    [self masonryMethod];
        [self masonryMethod1];

    return self;
}



- (void) systemMethod {
    UIView *superview = self;

    UIView *view1 = [[UIView alloc] init];

    view1.translatesAutoresizingMaskIntoConstraints = NO;  // Default:YES
    view1.backgroundColor = [UIColor grayColor];
    [superview addSubview:view1];

    UIEdgeInsets padding = UIEdgeInsetsMake(, , , );

    [NSLayoutConstraint activateConstraints:@[[NSLayoutConstraint constraintWithItem:view1
                                                                           attribute:NSLayoutAttributeTop
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:superview
                                                                           attribute:NSLayoutAttributeTop
                                                                          multiplier:
                                                                            constant:padding.top],

                                              [NSLayoutConstraint constraintWithItem:view1
                                                                           attribute:NSLayoutAttributeLeft
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:superview
                                                                           attribute:NSLayoutAttributeLeft
                                                                          multiplier:
                                                                            constant:padding.left],

                                              [NSLayoutConstraint constraintWithItem:view1
                                                                           attribute:NSLayoutAttributeBottom
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:superview
                                                                           attribute:NSLayoutAttributeBottom
                                                                          multiplier:
                                                                            constant:-padding.bottom],

                                              [NSLayoutConstraint constraintWithItem:view1
                                                                           attribute:NSLayoutAttributeRight
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:superview
                                                                           attribute:NSLayoutAttributeRight
                                                                          multiplier:
                                                                            constant:-padding.right],

                                              ]];

}


- (void) masonryMethod {
    UIView * superView = self;
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor yellowColor];
    [self addSubview:view];

//    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, -10, -10);
//    [view mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(superView.mas_top).offset(padding.top);
//        make.left.mas_equalTo(superView.mas_left).offset(padding.left);
//        make.right.mas_equalTo(superView.mas_right).offset(padding.right);
//        make.bottom.mas_equalTo(superView.mas_bottom).offset(padding.bottom);
//    }];
    UIEdgeInsets edgePadding = UIEdgeInsetsMake(, , , );
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView).insets(edgePadding);

        // 上下左右边距可以用edge代替。
        //    ❗️❗️❗️ 在使用Masonry添加约束之前,需要在addSubview之后才能使用,否则会导致崩溃。
    }];

}

- (void) masonryMethod1 {
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = UIColor.greenColor;
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@);  // NSNumber 赋值
        make.height.greaterThanOrEqualTo(@);
        make.center.equalTo(self);

    }];

    UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = UIColor.grayColor;
    [button setTitle:@"updateConstraint" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonclcik) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@);  // NSNumber 赋值
        make.height.greaterThanOrEqualTo(@);
        make.centerX.equalTo(view);
        make.top.equalTo(view).offset();
    }];

    self.testView = [[UIView alloc] init];
    self.testView.backgroundColor = UIColor.yellowColor;
    [self addSubview:self.testView];
    [self.testView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(view);
        make.left.equalTo(view.mas_right).offset();
        make.centerY.equalTo(view);
    }];
}

- (void)buttonclcik {
    // 更新cosntraint
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [UIView animateWithDuration: animations:^{
        [self layoutIfNeeded];
    }];
}

// 更新约束
- (void)updateConstraints {
//    [self.testView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.size.mas_equalTo(CGSizeMake(100, 30));
//        make.center.mas_equalTo(self).centerOffset(CGPointMake(-100, -100));
//
//    }];
    // super方法放在方法最后面执行
    [super updateConstraints];

}





@end
           

继续阅读