一.AutoLayout有两个重要的属性:

1.Content Compression Resistance 百度翻译(内容压缩抗力)

2.Content Hugging    百度翻译(内容拥抱)

  • Content Compression Resistance = 变大!(保持自己更大)
这个属性的优先级(Priority)越高,越不“容易”被压缩。也就是说,当整体的空间装不下所有的View的时候,Content Compression Resistance优先级越高的,现实的内容越完整。
 
[self.labelTwosetContentCompressionResistancePriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
 
  • Content Hugging = 抱紧!(保持自己更小)
这个属性的优先级越高,整个View就要越“抱紧”View里面的内容。也就是View的大小不会随着父级View的扩大而扩大。
[self.labelOnesetContentHuggingPriority:UILayoutPriorityDefaultHighforAxis:UILayoutConstraintAxisHorizontal];
 
    /**参数一:(UILayoutPriority)
      设置优先级等级,数值越大,优先级越高。
     UILayoutPriorityRequired           == 1000;
     UILayoutPriorityDefaultHigh        == 750;
     UILayoutPriorityDefaultLow         == 250;
     UILayoutPriorityFittingSizeLevel   == 50;
     */
   
    /**参数二:(UILayoutConstraintAxis)
       百度翻译(Axis:轴线。意思是你添加的优先级是Horizontal还是Vertical)
     UILayoutConstraintAxisHorizontal
     UILayoutConstraintAxisVertical
     */
二.代码部分
使用懒加载添加控件:详情请看本博客懒加载。
 #import "ViewController.h"
#import "Masonry.h" @interface ViewController () @property(nonatomic,strong) UILabel * labelOne;
@property(nonatomic,strong) UILabel * labelTwo; @end @implementation ViewController - (void)initUI
{
[self.view addSubview:self.labelOne];
[self.labelOne mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.view.mas_left).with.offset();
make.top.equalTo(self.view.mas_top).with.offset();
}]; [self.view addSubview:self.labelTwo];
[self.labelTwo mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_labelOne.mas_right);
make.right.equalTo(self.view.mas_right);
make.top.equalTo(self.view.mas_top).with.offset();
make.height.equalTo(_labelOne);
}]; [self.labelOne setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
/**参数一:(UILayoutPriority)
UILayoutPriorityRequired
UILayoutPriorityDefaultHigh
UILayoutPriorityDefaultLow
UILayoutPriorityFittingSizeLevel
*/ /**参数二:(UILayoutConstraintAxis)
UILayoutConstraintAxisHorizontal
UILayoutConstraintAxisVertical
*/
[self.labelTwo setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
}
- (void)viewDidLoad {
[super viewDidLoad]; [self initUI];
} - (UILabel *)labelOne
{
if (!_labelOne) {
self.labelOne = [[UILabel alloc] init];
self.labelOne.text = @"这是labelOne";
self.labelOne.backgroundColor = [UIColor redColor];
}
return _labelOne;
} - (UILabel *)labelTwo
{
if (!_labelTwo) {
self.labelTwo = [[UILabel alloc] init];
self.labelTwo.text = @"这是labelTwo";
self.labelTwo.backgroundColor = [UIColor greenColor];
}
return _labelTwo;
} @end

iOS-布局-Masonry-优先级的更多相关文章

  1. iOS开发-Masonry简易教程

    关于iOS布局自动iPhone6之后就是AutoLayOut,AutoLayOut固然非常好用,不过有时候我们需要在页面手动进行页面布局,VFL算是一种选择,如果对VFL不是很熟悉可以参考iOS开发- ...

  2. iOS 自动布局 Autolayout 优先级的使用

    一.约束的优先级 0.屏幕适配 发展历程 代码计算frame -> autoreszing(父控件和子控件的关系) -> autolayout(任何控件都可以产生关系) -> siz ...

  3. jQuery插件实现瀑布留布局masonry + infinitescroll 图片高度处理

    jQuery插件实现瀑布留布局masonry + infinitescroll . 使用官方的示例代码实际测试发现,当上传到服务器的时候,由于图片下载速度问题,导致图片高度不能被正确识别,从而造成层的 ...

  4. iOS - 布局重绘机制相关方法的研究

    iOS View布局重绘机制相关方法 布局 - (void)layoutSubviews - (void)layoutIfNeeded- (void)setNeedsLayout —————————— ...

  5. 第三方框架-纯代码布局:Masonry的简单使用

    Masonry是一个对系统NSLayoutConstraint进行封装的第三方自动布局框架,采用链式编程的方式提供给开发者API.系统AutoLayout支持的操作,Masonry都支持,相比系统AP ...

  6. Xcode iOS布局autolayout和sizeclass的使用

    一.关于自动布局(Autolayout) 在Xcode中,自动布局看似是一个很复杂的系统,在真正使用它之前,我也是这么认为的,不过事实并非如此. 我们知道,一款iOS应用,其主要UI组件是由一个个相对 ...

  7. iOS自动布局——Masonry详解

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由鹅厂新鲜事儿发表于云+社区专栏 作者:oceanlong | 腾讯 移动客户端开发工程师 前言 UI布局是整个前端体系里不可或缺的一环 ...

  8. iOS 使用Masonry介绍与使用实践:快速上手Autolayout

    介绍 Masonry 源码:https://github.com/Masonry/Masonry Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 ...

  9. iOS - 布局NSLayoutConstraint动画的实现

    抛出问题:为何在用到用到constraint的动画时以下代码无法实现动画的功能 ,没有动画直接刷新UI跳到80 - (void)touchesBegan:(NSSet<UITouch *> ...

  10. iOS:Masonry约束经验(19-03-21更)

    1.label约束: 1).只需约束x.y 点相关就行.宽高 长度相关不用约束,就算用boundingRectWithSize计算出来的,也可能不准. 如:top.bottom二选一,trailing ...

随机推荐

  1. Alfred 使用简介

    1.安装(不说了去 Google 吧) 2.基础快捷键:option+space 3.打开应用程序:Alfred 几乎是一切程序的入口,你再也不需要找妈妈要开始菜单了.用快捷键呼出Alfred,输入任 ...

  2. LICEcap – 灵活好用,GIF 屏幕录制工具

    LICEcap – 灵活好用,GIF 屏幕录制工具 http://www.appinn.com/licecap/

  3. 一致性hash和solr千万级数据分布式搜索引擎中的应用

    互联网创业中大部分人都是草根创业,这个时候没有强劲的服务器,也没有钱去买很昂贵的海量数据库.在这样严峻的条件下,一批又一批的创业者从创业中 获得成功,这个和当前的开源技术.海量数据架构有着必不可分的关 ...

  4. Speech两种使用方法

    COM组件使用speech: public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVo ...

  5. nginx 重写 rewrite 基础及实例

    nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否 ...

  6. Codeforces Round #382 (Div. 2) D. Taxes 哥德巴赫猜想

    D. Taxes 题目链接 http://codeforces.com/contest/735/problem/D 题面 Mr. Funt now lives in a country with a ...

  7. First MarkDown Blog

    #First MarkDown Blog ##Title1 ##Tiltle2

  8. 39. Volume Rendering Techniques

    Milan Ikits University of Utah Joe Kniss University of Utah Aaron Lefohn University of California, D ...

  9. Eclipse远程调试HDP源代码

    使用的是自己编译的HDP2.3.0的源代码编译的集群,此文介绍如何使用Eclipse远程调试Hadoop内核源代码,以调试namenode为例进行介绍. 在/usr/hdp/2.3.0.0-2557/ ...

  10. timer.scheduleAtFixedRate和timer.schedule的实验

    基础代码: Calendar  currentTime = Calendar.getInstance(); currentTime.setTime(new Date()); int  currentH ...