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];

    }];
}
时间: 2024-08-29 00:11:40

Masonry设置约束优先级[转]的相关文章

详解intrinsicContentSize 及 约束优先级/content Hugging/content Compression Resistance [转]

在了解intrinsicContentSize之前,我们需要先了解2个概念: AutoLayout在做什么 约束优先级是什么意思. 如果不了解这两个概念,看intinsic content size没有任何意义. 注:由于上面这几个概念都是针对UIView或其子类(UILabel,UIImageView等等)来说的.所以下文中都用UIView指代. AutoLayout在做什么 – 一个UIView想要显示在屏幕中,仅须有2个需要确定的元素,一是位置,二是大小.只要2者确定,UIView就可以正

详解intrinsicContentSize 及 约束优先级/content Hugging/content Compression Resistance

在了解intrinsicContentSize之前,我们需要先了解2个概念: AutoLayout在做什么 约束优先级是什么意思. 如果不了解这两个概念,看intinsic content size没有任何意义. 注:由于上面这几个概念都是针对UIView或其子类(UILabel,UIImageView等等)来说的.所以下文中都用UIView指代. AutoLayout在做什么 – 一个UIView想要显示在屏幕中,仅须有2个需要确定的元素,一是位置,二是大小.只要2者确定,UIView就可以正

masonry 设置控件抗压缩及抗拉伸

使用masonry正常设置约束时两个label的显示是下图 添加代码设置蓝色label的抗压缩属性后( [self.missionNameLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; )效果如下 同理使用代码( [self.missionNameLabel setContentCompressionResistanc

Swift - 自动布局库SnapKit的使用详解3(约束优先级,约束做动画)

1,约束优先级我们使用SnapKit的时候,还可以定义约束的优先级.这样当约束出现冲突的时候,优先级高的约束覆盖优先级低的约束.具体优先级可以放在约束链的结束处. (1)可以设置如下几种优先级 priorityLow():设置低优先级,优先级为250priorityMedium():设置中优先级,优先级为500(这个也就是默认的优先级)priorityHigh():设置高优先级,优先级为750priority():可以设置任意的优先级,接受的参数是0-1000的数字.比如:priority(60

数据库(学习整理)----3--Oracle创建表和设置约束

BBS论坛表设计 包含的表:BBSusers(用户表),BBSsection(版块表),BBStopic(主贴表),BBSreply(跟帖表) 表结构 1)BBSusers 字段名 字段说明 数据类型 字段约束 备注 U_ID 用户ID number 主键,非空 非空 UName 用户名 Varchar2(32) 非空 UPassword 密码 Varchar2(16) 默认6个8 UEmail email Varchar2(32) 默认[email protected],必须有“@”和“.”,

C#的线程类之Thread类以及对为什么设置线程优先级却不先执行这一问题本身的认识

1.C#对线程进行操作时,通过Thread类,可以对线程进行创建.挂起.恢复.休眠.终止及设置优先级. Thread类位于System.Threading命名空间下,该命名空间还包含一个ThreadPool类(允许用户使用系统提供的线程池)和一个Timer类(在线程池上执行回调方法) 在线程运行期间,不同的时刻会表现为不同的状态,但它总是处于由ThreadState定义的一个或多个状态中.用户可以通过使用ThreadPriority枚举为线程定义优先级,但不能保证操系统会接收该优先级 2.Thr

ceSetThreadPriority设置线程优先级~!

ceSetThreadPriority 一直采用SetThreadPriority,结果今天发帖询问线程时间问题,才突然顿悟...发现SetThreadPriority仅仅设置248-255,也就是说就算是设为最高优先级,顶多是248....差好远呢...而ceSetThreadPriority的设置范围是0-255,最高为0!!!我就说我的线程设置的优先级怎么没什么用......NYYD... 初始化代码中的关键 Win32 API 调用是对 CeSetThreadPriority 的调用.此

Masonry 一个约束第三方类库

Masonry这个东东相当相当的牛掰,牛掰在哪呢,好用,相当的简单容易用.不愧为2000多个赞的第三方. 来让我们看看具体用法. UIView *gr = UIView.new; gr.backgroundColor = [UIColor greenColor]; UIView *re = UIView.new; re.backgroundColor = [UIColor redColor]; UIView *ye = UIView.new; ye.backgroundColor = [UICo

Masonry 添加约束要注意顺序

对一个视图添加约束,其依赖的约束必须先已经存在,不能依赖该代码后的约束,否则造成不可预料的结果,如下代码能达到预期效果 - (void)makeConstraints { __weak typeof(self) weakSelf = self; [self.photoMoreButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weakSelf.photoButton).with.offset(-6