Masonry使用注意篇

Masonry使用注意篇

简要less

自动布局最重要的是约束:UI元素间关系的数学表达式。约束包括尺寸、由优先级和阈值管理的相对位置。它们是添加剂,可能致使约束冲突 、约束不足形成布局没法肯定 。这两种状况都会产生异常。ide

使用前:AutoLayout关于更新的几个方法的区别

  • setNeedsLayout:告知页面须要更新,可是不会马上开始更新。执行后会马上调用layoutSubviews。
  • layoutIfNeeded:告知页面布局马上更新。因此通常都会和setNeedsLayout一块儿使用。若是但愿马上生成新的frame须要调用此方法,利用这点通常布局动画能够在更新布局后直接使用这个方法让动画生效。
  • layoutSubviews:系统重写布局
  • setNeedsUpdateConstraints:告知须要更新约束,可是不会马上开始
  • updateConstraintsIfNeeded:告知马上更新约束
  • updateConstraints:系统更新约束

使用

1. 基本使用布局

  • mas_makeConstraints:添加约束
  • mas_updateConstraints:更新约束、亦可添加新约束
  • mas_remakeConstraints:重置以前的约束动画

  • multipler属性表示约束值为约束对象的乘因数, dividedBy属性表示约束值为约束对象的除因数,可用于设置view的宽高比ui

    // 进行屏幕的适配的时候,每每须要根据屏幕宽度来适配一个相应的高度,在此推荐使用以下约束的方式来进行控件的适配
    [self.topView addSubview:self.topInnerView];
    [self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.topView.mas_height).dividedBy(3);
        make.width.and.height.lessThanOrEqualTo(self.topView);
        make.width.and.height.equalTo(self.topView).with.priorityLow();
        make.center.equalTo(self.topView);
    }];
  • priorityLow()设置约束优先级
  • #define MAS_SHORTHAND_GLOBALS使用全局宏定义,可使equalTo等效于mas_equalTo
  • #define MAS_SHORTHAND使用全局宏定义, 能够在调用masonry方法的时候不使用mas_前缀
// 这里注意到一个地方,就是当使用了这个全局宏定义以后,发现能够有个类`NSArray+MASAdditions.h`,看了以后发现能够
self.buttonViews = @[ raiseButton, lowerButton, centerButton ];
// 以后能够在updateConstraints 方法中
- (void)updateConstraints {
   [self.buttonViews updateConstraints:^(MASConstraintMaker *make) {
        make.baseline.equalTo(self.mas_centerY).with.offset(self.offset);
    }];
    [super updateConstraints];  
}
  • 动态修改视图约束:
    // 建立视图约束
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
          self.animatableConstraint = make.edges.equalTo(superview).insets(paddingInsets).priorityLow();
    ]];
    // 更改约束 (另外一处方法中)
    UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
    self.animatableConstraint.insets = paddingInsets;
    [self layoutIfNeeded];
  • debug模式:
    // 对某个view添加key值
    greenView.mas_key = @"greenView";
    // 或者以下顺序
    MASAttachKeys(greenView, redView, blueView, superview);
    // 一样的对每条约束亦能够添加key
    make.height.greaterThanOrEqualTo(@5000).key(@"ConstantConstraint");
  • preferredMaxLayoutWidth: 多行label的约束问题
// 已经确认好了位置
// 在layoutSubviews中确认label的preferredMaxLayoutWidth值
- (void)layoutSubviews {
    [super layoutSubviews];
    // 你必须在 [super layoutSubviews] 调用以后,longLabel的frame有值以后设置preferredMaxLayoutWidth
    self.longLabel.preferredMaxLayoutWidth = self.frame.size.width-100;
    // 设置preferredLayoutWidth后,须要从新布局
    [super layoutSubviews];
}
  • scrollView使用约束的问题:原理经过一个contentView来约束scrollView的contentSize大小,也就是说以子控件的约束条件,来控制父视图的大小
// 1. 控制scrollView大小(显示区域)
[self.scrollView makeConstraints:^(MASConstraintMaker *make) {
     make.edges.equalTo(self.view);
}];
// 2. 添加一个contentView到scrollView,而且添加好约束条件
[contentView makeConstraints:^(MASConstraintMaker *make) {
     make.edges.equalTo(self.scrollView);
     // 注意到此处的宽度约束条件,这个宽度的约束条件是比添加项
     make.width.equalTo(self.scrollView);
}];
// 3. 对contentView的子控件作好约束,达到能够控制contentView的大小
  • 新方法:2个或2个以上的控件等间隔排序
/**
 *  多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值
 *
 *  @param axisType        轴线方向
 *  @param fixedSpacing    间隔大小
 *  @param leadSpacing     头部间隔
 *  @param tailSpacing     尾部间隔
 */
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType 
                    withFixedSpacing:(CGFloat)fixedSpacing l
                          eadSpacing:(CGFloat)leadSpacing 
                         tailSpacing:(CGFloat)tailSpacing;

/**
 *  多个固定大小的控件的等间隔排列,变化的是间隔的空隙
 *
 *  @param axisType        轴线方向
 *  @param fixedItemLength 每一个控件的固定长度或者宽度值
 *  @param leadSpacing     头部间隔
 *  @param tailSpacing     尾部间隔
 */
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType 
                 withFixedItemLength:(CGFloat)fixedItemLength 
                         leadSpacing:(CGFloat)leadSpacing 
                         tailSpacing:(CGFloat)tailSpacing;

使用方法很简单,由于它是NSArray的类扩展:spa

//  建立水平排列图标 arr中放置了2个或连个以上的初始化后的控件
//  alongAxis 轴线方向   固定间隔     头部间隔      尾部间隔
[arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:5 tailSpacing:5];
[arr makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(@60);
       make.height.equalTo(@60);
}];

2. 注意事项debug

  • 约束视图对象只有在被addSubview以后,才能给视图添加约束
  • 当你的全部约束都在 updateConstraints 内调用的时候,你就须要在此调用此方法,由于 updateConstraints方法是须要触发的
// 调用在view 内部,而不是viewcontroller
+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

/**
 *  苹果推荐 约束 增长和修改 放在此方法种
 */
- (void)updateConstraints {
    [self.growingButton 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);
    }];
    //最后记得回调super方法
    [super updateConstraints];
}
  • 若是想要约束变换以后实现动画效果,则须要执行以下操做
    // 通知须要更新约束,可是不当即执行
    [self setNeedsUpdateConstraints];
    // 当即更新约束,以执行动态变换
    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];
    // 执行动画效果, 设置动画时间
    [UIView animateWithDuration:0.4 animations:^{
       [self layoutIfNeeded];
    }];
相关文章
相关标签/搜索