Masonry设置约束优先级[转]

Posted YouNeedCourage

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Masonry设置约束优先级[转]相关的知识,希望对你有一定的参考价值。

原文:http://www.jianshu.com/p/b0e1797036fe

 

#####前言:以前看到那种布局好的界面,当其中一个控件消失后,其余控件自动调整约束,还不知道怎么实现。 下去学习了一下,其实就是设置约束有先级的问题。 下面直接上代码,布局用的是Masonry

pragma mark - 1. 先看看效果哈
技术分享
纯代码约束优先级.gif
pragma mark - 2. 代码实现加简单注释
#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()

//设置三个View
/**<#XXX#>*/
@property (nonatomic , strong) UIView * orangeView;

/**<#XXX#>*/
@property (nonatomic , strong) UIView * yellowView;

/**<#XXX#>*/
@property (nonatomic , strong) UIView * greenView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.添加三个View
    UIView *orangeView = [[UIView alloc]init];
    orangeView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:orangeView];
    self.orangeView = orangeView;
    //2.
    UIView *yellowView = [[UIView alloc]init];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:yellowView];
    self.yellowView = yellowView;
    //3.
    UIView *greenView = [[UIView alloc]init];
    greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:greenView];
    self.greenView = greenView;

//配置约束
    [self setUpRestrain];
}

//配置约束
-(void)setUpRestrain
{
     __weak typeof( self) weakSelf = self;

#对于橙色View只需正常设置约束就好
  [self.orangeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.left.offset(10);
        make.top.offset(50);
    }];
#黄色View只会发生一次变化,就多设一个优先级较低的约束就好  
  [self.yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.width.height.mas_equalTo(100);
        make.left.equalTo(weakSelf.orangeView.mas_right).offset(20);
        make.top.offset(50);
 #当橙色View消失后,黄色View缺少左边约束,所以给其加一个优先级更低的左边约束。当第一个左边约束失效后,这个约束就起作用了
       make.left.equalTo(weakSelf.view.mas_left).offset(20).priority(300);

    }];
#同理绿色View的低级约束就得设置两个   
 [self.greenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.left.equalTo(weakSelf.yellowView.mas_right).offset(20);
        make.top.offset(50);
       make.left.equalTo(weakSelf.orangeView.mas_right).offset(20).priority(300);
        make.left.equalTo(weakSelf.view.mas_left).offset(20).priority(250);
    }];
}

//删除黄色View
- (IBAction)cancelYellowBtn:(id)sender {
#这里有个知识点:用约束布局实现动画,布局代码写在外面,然后调用强制布局方法写在UIView动画里面
    [self.yellowView removeFromSuperview];
    [UIView animateWithDuration:0.5 animations:^{
      //强制刷新布局
        [self.view layoutIfNeeded];
    }];

}
//删除橙色View
- (IBAction)cancelGreenBtn:(id)sender {
    [self.orangeView removeFromSuperview];
    [UIView animateWithDuration:0.5 animations:^{
        //强制刷新布局
        [self.view layoutIfNeeded];

    }];
}

以上是关于Masonry设置约束优先级[转]的主要内容,如果未能解决你的问题,请参考以下文章

Masonry库的使用

iOS Masonry 设置布局优先级。

Masonry部分用法(控件数组等间隔排序)

Masonry 代码添加约束

Masonry约束立即生效跟约束更新

ios入门Masonry约束立即生效跟约束更新