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

    }];
}
posted @   YouNeedCourage  阅读(9758)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2016-04-27 推送证书配置指南
点击右上角即可分享
微信分享提示