一.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. Redis+Django(Session,Cookie)的用户系统

    一.Django authentication django authentication提供了一个便利的user api接口,无论在py中 request.user,参见Request and re ...

  2. protocol buffer

    下载与说明:https://github.com/google/protobuf Google Protocol Buffer 的使用和原理:http://www.ibm.com/developerw ...

  3. [转]揭秘webdriver实现原理

    转自:http://www.cnblogs.com/timsheng/archive/2012/06/12/2546957.html 通过研究selenium-webdriver的源码,笔者发现其实w ...

  4. mybatis热加载的实现

    最近在使用mybatis,由于是刚刚开始用,用的并不顺手,目前是感觉有2个地方非常的不好用: 1.mybatis调试不方便 由于dao层只有接口,实现只是一个map的xml文件,想加断点都没有地方加, ...

  5. iOS中的webView加载HTML

    在日常开发中,我们为了效率会用到很多很多的WebView,比如在做某个明细页面的时候我们返回给你的可能是一个html字符串,我们就需要将当前字符串展示到webView上面,所以我们对HTML标签需要有 ...

  6. RSA算法 Android JAVA C#互通

    RSA算法属非对称加密算法,在实际使用中,往往客户端使用公钥进行加密传递敏感数据,服务端server使用私钥进行解密,这样防止中间人从网络获取敏感数据的明文. Android端主要代码如下: pack ...

  7. ASP.NET弹出显示ex.Message异常信息 存在换行符和回车符处理办法。

    1.把ex.Message换成任意字符串,检验在catch语句块中可以用Response.Write方法显示对话框.结果显示成功,说明问题就出在ex.Message上. 2.在程序中下断点,可以看到e ...

  8. javascript 搜索二叉树

    function Tree() { this.root = null; } Tree.prototype = { constructor: Tree, addItem: function(value) ...

  9. Navi.Soft30.产品.DataWindowNet.操作手册

    1概述 1.1功能简介 Sybase公司的PowerBuilder开发工具,在以前VS工具没有成事以前,是相当风光的.微软都要与其合作,学习它Db方面的技术,才成就了SQLServer数据库.PB开发 ...

  10. C# WinForm 技巧八:界面开发之“WeifenLuo.WinFormsUI.Docking+OutLookBar” 使用

    概述      最近几天一直在关注WinFrom方面的文章主要还是园子里伍华聪的博客,在看看我们自己写的项目差不忍赌啊,有想着提炼一下项目的公共部分,公共部分有分为 界面,类库两方面,今天主要是把界面 ...