Masonry

  • mas_makeConstraints使用最多,做初始约束,只负责新增约束

  • mas_updateConstraints针对make中的约束做更新,一般不添加新约束,只是对于原约束数值的修改.

  • mas_remakeConstraints清除之前所有的约束,采用里面设置的最新约束,常用于动画之后新位置的约束设定

  • 优先级高.priorityHigh,优先级低.priorityLow,通常用于根据条件判断来设定优先响应哪种约束

  • multipliedBy(x)= *x,dividedBy(x)= /x

  • 实现动画时,更新约束后调用layoutIfNeeded

make.edges.mas_equalTo(UIEdgeInsetsMake(10, 10, 10, 10));
// edges就是相当于top-left-bottom-right
// top和left里的offset为正数

// 那么为什么bottom和right里的offset是负数呢? 因为这里计算的是绝对的数值 计算的bottom需要小

于sv的底部高度 所以要-10 同理用于right

// 有意思的地方是and和with 其实这两个函数什么事情都没做,可以省略的
/* 等价于
  make.top.equalTo(weakSelf.sv).with.offset(10);
  make.left.equalTo(weakSelf.sv).with.offset(10);
  make.bottom.equalTo(weakSelf.sv).with.offset(-10);
  make.right.equalTo(weakSelf.sv).with.offset(-10);


  • UIScrollView是一个有点特殊的view,对于在里面放其他view,最好的做法是先放一个containerView设置edges相等,然后在此view上添加subview.

  • 模仿系统的UITabBarController的底部View,可以设定个数和间隔:

- ( void )simulateSystemTabBarWithButtonCount:(NSInteger)count withSpace:(CGFloat) space {
     [self.view showPlaceHolder];
     self.view.backgroundColor = [UIColor grayColor];
     NSMutableArray *viewArray = [NSMutableArray arrayWithCapacity:10];
     
     for ( int i = 0; i < count; i++) {
         UIView *view = [UIView  new ];
         view.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )
                                           saturation:( arc4random() % 128 / 256.0 ) + 0.5
                                           brightness:( arc4random() % 128 / 256.0 ) + 0.5
                                                alpha:1];;
         [view showPlaceHolder];
         [self.view addSubview:view];
         [viewArray addObject:view];
     }
     UIView *lastView = viewArray.lastObject;
     for ( int i = 0; i < count; i++) {
         UIView *view = (UIView *)viewArray[i];
         [view mas_makeConstraints:^(MASConstraintMaker *make) {
             make.bottom.equalTo(view.superview);
             make.height.equalTo(@49);
             make.width.equalTo(lastView);
             if (i == 0) {
                 make.left.mas_equalTo(view.superview).offset(space);
             else {
                 UIView *frontView = (UIView *)viewArray[i-1];
                 make.left.equalTo(frontView.mas_right).offset(space);
                 (i == viewArray.count - 1) ? make.right.mas_equalTo(view.superview).offset(-space) : nil;
             }
         }];
     }
}



你可能感兴趣的:(Masonry)