必威体育Betway必威体育官网
当前位置:首页 > IT技术

Masonry

时间:2019-11-08 05:44:26来源:IT技术作者:seo实验室小编阅读:73次「手机版」
 

masonry

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(10, 10, 10, 10);

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

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

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

                                              [NSLayoutConstraint constraintWithItem:view1
                                                                           attribute:NSLayoutAttributeRight
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:superview
                                                                           attribute:NSLayoutAttributeRight
                                                                          multiplier:1
                                                                            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(10, 10, 10, 10);
    [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(@100);  // NSNumber 赋值
        make.height.greaterThanOrEqualTo(@30);
        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(@100);  // NSNumber 赋值
        make.height.greaterThanOrEqualTo(@30);
        make.centerX.equalTo(view);
        make.top.equalTo(view).offset(30);
    }];

    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(30);
        make.centerY.equalTo(view);
    }];
}

- (void)buttonclcik {
    // 更新cosntraint
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfneeded];
    [UIView animateWithDuration:0.4 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

文章最后发布于: 2018-06-21 18:06:31

相关阅读

Masonry(一)

简介 Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wid

分享到:

栏目导航

推荐阅读

热门阅读