个人喜欢用纯代码写东西,其中用到*多的就是Masonry,我整理一些使用过程中一些点,方便以后使用.(基本的语法就不说了)

首先说几点:

  1. 我一般将数值类型的约束用mas_equalTo,而相对于某个控件,或者某个控件的某个约束,我会使用equalTo,如:
    make.size.mas_equalTo(CGSizeMake(100, 100));
    make.center.equalTo(weakSelf.view);
  2. setNeedsLayout:告知页面需要更新,但是不会立刻开始更新。执行后会立刻调用layoutSubviews。
    layoutIfNeeded:告知页面布局立刻更新。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
    layoutSubviews:系统重写布局
    setNeedsUpdateConstraints:告知需要更新约束,但是不会立刻开始
    updateConstraintsIfNeeded:告知立刻更新约束
    updateConstraints:系统更新约束
  3. - (void)updateViewConstraints ViewController的View在更新视图布局时,会先调用ViewController的updateViewConstraints 方法。我们可以通过重写这个方法去更新当前View的内部布局,而不用再继承这个View去重写-updateConstraints方法。我们在重写这个方法时,务必要调用 super 或者 调用当前View的 -updateConstraints 方法。

1. 视图居中显示

  1. // 防止block中的循环引用
  2. __weak typeof(self) weakSelf = self;
  3. UIView* view = [UIView new];
  4. view.backgroundColor = [UIColor brownColor];
  5. [self.view addSubview:view];
  6. //使用mas_makeConstraints添加约束
  7. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  8. // 添加大小约束(make就是要添加约束的控件view)
  9. make.size.mas_equalTo(CGSizeMake(200, 200));
  10. // 添加居中约束(居中方式与self相同)
  11. make.center.equalTo(weakSelf.view);
  12. }];

2. 两个视图等宽高边距

  1. UIView* blackView = [UIView new];
  2. blackView.backgroundColor = [UIColor blackColor];
  3. [self.view addSubview:blackView];
  4. [blackView mas_makeConstraints:^(MASConstraintMaker *make) {
  5. //添加约束大小
  6. make.size.mas_equalTo(CGSizeMake(100, 100));
  7. //在 左,上 添加约束 (左、上约束都是20)
  8. make.left.and.top.mas_equalTo(20);
  9. }];
  10. UIView* grayView = [UIView new];
  11. grayView.backgroundColor = [UIColor lightGrayColor];
  12. [self.view addSubview:grayView];
  13. [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
  14. // 大小、上边距约束与黑色view相同
  15. make.size.and.top.equalTo(blackView);
  16. // 添加右边距约束(这里的间距是有方向性的,左、上边距约束为正数,右、下边距约束为负数)
  17. make.right.mas_equalTo(20);
  18. }];

3. 键盘弹出和收回

  1. – (void)dealloc {
  2. [[NSNotificationCenter defaultCenter] removeObserver:self];
  3. }
  4. – (void)viewDidLoad {
  5. [super viewDidLoad];
  6. // Do any additional setup after loading the view.
  7. __weak typeof(self) weakSelf = self;
  8. _textField = [UITextField new];
  9. _textField.backgroundColor = [UIColor redColor];
  10. [self.view addSubview:_textField];
  11. [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
  12. //left,right,centerx,y 不能共存只能有其二
  13. make.left.mas_equalTo(20);
  14. // make.right.mas_equalTo(-60);
  15. make.centerX.equalTo(weakSelf.view);
  16. make.height.mas_equalTo(40);
  17. make.bottom.mas_equalTo(0);
  18. }];
  19. // 注册键盘通知
  20. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
  21. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
  22. }
  23. – (void)keyboardWillChangeFrameNotification:(NSNotification *)notification {
  24. // 获取键盘基本信息(动画时长与键盘高度)
  25. NSDictionary *userInfo = [notification userInfo];
  26. CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  27. CGFloat keyboardHeight = CGRectGetHeight(rect);
  28. CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  29. // 修改下边距约束
  30. [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
  31. make.bottom.mas_equalTo(-keyboardHeight);
  32. }];
  33. // 更新约束
  34. [UIView animateWithDuration:keyboardDuration animations:^{
  35. [self.view layoutIfNeeded];
  36. }];
  37. }
  38. – (void)keyboardWillHideNotification:(NSNotification *)notification {
  39. // 获得键盘动画时长
  40. NSDictionary *userInfo = [notification userInfo];
  41. CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  42. // 修改为以前的约束(距下边距0)
  43. [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
  44. make.bottom.mas_equalTo(0);
  45. }];
  46. // 更新约束
  47. [UIView animateWithDuration:keyboardDuration animations:^{
  48. [self.view layoutIfNeeded];
  49. }];
  50. }
  51. – (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  52. [super touchesBegan:touches withEvent:event];
  53. [self.view endEditing:YES];
  54. }

4. 三控件等宽间距

方法一:

array 的 mas_distributeViewsAlongAxis withFixedSpacing 变化的是控件 长度或宽度

定义一个存放三个控件的数组NSArray *array;
array = @[greenView,redView,blueView];

直接调用下面的方法:

  1. - (void)getHorizontalone
  2. {
  3. //方法一,array 的 mas_distributeViewsAlongAxis
  4. /**
  5. * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值
  6. *
  7. * @param axisType 轴线方向
  8. * @param fixedSpacing 间隔大小
  9. * @param leadSpacing 头部间隔
  10. * @param tailSpacing 尾部间隔
  11. */
  12. // MASAxisTypeHorizontal 水平
  13. // MASAxisTypeVertical 垂直
  14. [arrayList mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
  15. withFixedSpacing:20
  16. leadSpacing:5
  17. tailSpacing:5];
  18. [arrayList mas_makeConstraints:^(MASConstraintMaker *make) {
  19. make.top.mas_equalTo(60);
  20. make.height.mas_equalTo(100);
  21. }];

}

方法二:

array de mas_distributeViewsAlongAxis withFixedItemLength 控件size不变,变化的是间隙

  1. - (void)getVertical
  2. {
  3. /**
  4. * 多个固定大小的控件的等间隔排列,变化的是间隔的空隙
  5. *
  6. * @param axisType 轴线方向
  7. * @param fixedItemLength 每个控件的固定长度或者宽度值
  8. * @param leadSpacing 头部间隔
  9. * @param tailSpacing 尾部间隔
  10. */
  11. [arrayList mas_distributeViewsAlongAxis:MASAxisTypeVertical
  12. withFixedItemLength:60
  13. leadSpacing:40
  14. tailSpacing:10];
  15. [arrayList mas_makeConstraints:^(MASConstraintMaker *make) {
  16. // make.top.mas_equalTo(100);
  17. // make.height.mas_equalTo(100);
  18. make.left.mas_equalTo(20);
  19. make.right.mas_equalTo(-20);
  20. }];

}

以上俩方法都在NSArray+MASAdditions
方法三:直接设置multiplier实现等间距
  1. for (NSUInteger i = 0; i < 4; i++) {
  2. UIView *itemView = [self getItemViewWithIndex:i];
  3. [_containerView addSubview:itemView];
  4. [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
  5. make.width.and.height.equalTo(@(ITEM_SIZE));
  6. make.centerY.equalTo(_containerView.mas_centerY);
  7. make.centerX.equalTo(_containerView.mas_right).multipliedBy(((CGFloat)i + 1) / ((CGFloat)ITEM_COUNT + 1));
  8. }];
  9. }
方法四: 利用透明等宽度的SpaceView实现等间距
  1. UIView *lastSpaceView = [UIView new];
  2. lastSpaceView.backgroundColor = [UIColor greenColor];
  3. [_containerView1 addSubview:lastSpaceView];
  4. [lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
  5. make.left.and.top.and.bottom.equalTo(_containerView1);
  6. }];
  7. for (NSUInteger i = 0; i < ITEM_COUNT; i++) {
  8. UIView *itemView = [self getItemViewWithIndex:i];
  9. [_containerView1 addSubview:itemView];
  10. [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
  11. make.height.and.width.equalTo(@(ITEM_SIZE));
  12. make.left.equalTo(lastSpaceView.mas_right);
  13. make.centerY.equalTo(_containerView1.mas_centerY);
  14. }];
  15. UIView *spaceView = [UIView new];
  16. spaceView.backgroundColor = [UIColor greenColor];
  17. [_containerView1 addSubview:spaceView];
  18. [spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
  19. make.left.equalTo(itemView.mas_right).with.priorityHigh(); // 降低优先级,防止宽度不够出现约束冲突
  20. make.top.and.bottom.equalTo(_containerView1);
  21. make.width.equalTo(lastSpaceView.mas_width);
  22. }];
  23. lastSpaceView = spaceView;
  24. }
  25. [lastSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.right.equalTo(_containerView1.mas_right);
  27. }];
5. 动态改变字体宽度

和面方法4一样,利用spaceView来实现

  1. UIView* bgView = [[UIView alloc]init];
  2. bgView.backgroundColor = [UIColor yellowColor];
  3. [self.view addSubview:bgView];
  4. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  5. make.left.and.right.mas_equalTo(0);
  6. make.top.mas_equalTo(@100);
  7. make.height.mas_equalTo(@100);
  8. }];
  9. listText = @[@”北京”,@”地大吴波啊”,@”你大爷”,@”我们的爱哎哎”];
  10. UIView *lastSpaceView = nil;
  11. for(int i = 0 ; i < listText.count; i ++)
  12. {
  13. UILabel* label = [UILabel new];
  14. label.text = listText[i];
  15. label.backgroundColor = RANDOMCOLOR;
  16. [bgView addSubview:label];
  17. UIView* lineView = [UIView new];
  18. lineView.backgroundColor = [UIColor redColor];
  19. [bgView addSubview:lineView];
  20. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  21. make.top.bottom.mas_equalTo(0);
  22. if (lastSpaceView)
  23. {
  24. NSLog(@”存在 lastView”);
  25. make.left.equalTo(lastSpaceView.mas_right).mas_offset(@20);
  26. }else
  27. {
  28. NSLog(@”不存在存在 lastView”);
  29. make.left.equalTo(bgView.mas_left);
  30. }
  31. make.height.equalTo(bgView);
  32. }];
  33. lastSpaceView = label;
  34. [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.top.and.bottom.mas_equalTo(0);
  36. make.width.mas_equalTo(1);
  37. make.left.mas_equalTo(label.mas_right).mas_offset(@10);
  38. }];
  39. }
效果图:
%title插图%num

6. 父视图的高度,是里面俩控件高度的和
  1. UIView* bgView = [UIView new];
  2. bgView.backgroundColor = [UIColor purpleColor];
  3. [self.view addSubview:bgView];
  4. UILabel* titleLab = [UILabel new];
  5. titleLab.backgroundColor = [UIColor redColor];
  6. titleLab.textAlignment = NSTextAlignmentCenter;
  7. titleLab.font = [UIFont systemFontOfSize:15.f];
  8. titleLab.text = @”曹操——《短歌行》”;
  9. [bgView addSubview:titleLab];
  10. UILabel* contentLab = [UILabel new];
  11. contentLab.numberOfLines = 0 ;
  12. contentLab.textAlignment = NSTextAlignmentCenter;
  13. contentLab.backgroundColor = [UIColor brownColor];
  14. contentLab.font = [UIFont systemFontOfSize:13.f];
  15. contentLab.text = @” 对酒当歌,人生几何? 譬如朝露,去日苦多。\n 慨当以慷,忧思难忘。 何以解忧?唯有杜康。\n 青青子衿,悠悠我心。 但为君故,沉吟至今。\n 呦呦鹿鸣,食野之苹。 我有嘉宾,鼓瑟吹笙。\n 明明如月,何时可掇? 忧从中来,不可断*。\n 越陌度阡,枉用相存。 契阔谈宴,心念旧恩。\n 月明星稀,乌鹊南飞。 绕树三匝,何枝可依?\n 山不厌高,海不厌深。 周公吐哺,天下归心。”;
  16. [bgView addSubview:contentLab];
  17. //思路: 父视图的上间距等于title的上间距,父视图的下间距等于content的下间距
  18. __weak typeof(self) weakSelf = self;
  19. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  20. make.left.mas_offset(@30);
  21. make.right.mas_offset(@30);
  22. make.centerY.equalTo(weakSelf.view);
  23. }];
  24. [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  25. make.left.top.right.mas_equalTo(@0);
  26. }];
  27. [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.left.right.mas_equalTo(@0);
  29. make.top.equalTo(titleLab.mas_bottom).mas_offset(@10);
  30. make.bottom.equalTo(bgView);
  31. }];

效果图:

%title插图%num

以后慢慢更新,记录方便以后使用

文/栋飞

//一些扒的别人的记录

自适应布局允许将宽度或高度设置为固定值.如果你想要给视图一个*小或*大值,你可以这样:

//width >= 200 && width <= 400make.width.greaterThanOrEqualTo(@200);make.width.lessThanOrEqualTo(@400)

约束的优先级

.priority允许你指定一个精确的优先级,数值越大优先级越高.*高1000.
.priorityHigh等价于 UILayoutPriorityDefaultHigh .优先级值为 750.
.priorityMedium介于高优先级和低优先级之间,优先级值在 250~750之间.
.priorityLow等价于 UILayoutPriorityDefaultLow , 优先级值为 250.

优先级可以在约束的尾部添加:

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();
make.top.equalTo(label.mas_top).with.priority(600);

center 中心

//使 centerX和 centerY = button1
make.center.equalTo(button1)

//使 centerX = superview.centerX – 5, centerY = superview.centerY + 10make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

指定宽度为父视图的 1/4.

make.width.equalTo(superview).multipliedBy(0.25);