转:AutoLayout中的Content Hugging 和 Content Compression Resistance
在AutoLayout的学习中有两个概念官方文档讲述的不是很清楚,今天花费了2个小时的时间研究了一下,在此总结一下。
Content Hugging 和 Content Compression Resistance
这两个属性对有intrinsic content size的控件(例如button,label)非常重要。通俗的讲,具有intrinsic content size的控件自己知道(可以计算)自己的大小,例如一个label,当你设置text,font之后,其大小是可以计算到的。关于intrinsic content size官方的解释:
Custom views typically have content that they display of which the layout system is unaware. Overriding this method allows a custom view to communicate to the layout system what size it would like to be based on its content. This intrinsic size must be independent of the content frame, because there’s no way to dynamically communicate a changed width to the layout system based on a changed height, for example.
好了,了解了intrinsic content size的概念之后,下面就重点讨论Content Hugging 和 Content Compression Resistance了。
UIView中关于Content Hugging 和 Content Compression Resistance的方法有:
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
大概的意思就是设置优先级的。
Hugging priority 确定view有多大的优先级阻止自己变大。
Compression Resistance priority确定有多大的优先级阻止自己变小。
很抽象,其实content Hugging就是要维持当前view在它的optimal size(intrinsic content size),可以想象成给view添加了一个额外的width constraint,此constraint试图保持view的size不让其变大:
view.width <= optimal size
此constraint的优先级就是通过上面的方法得到和设置的,content Hugging默认为250.
Content Compression Resistance就是要维持当前view在他的optimal size(intrinsic content size),可以想象成给view添加了一个额外的width constraint,此constraint试图保持view的size不让其变小:
view.width >= optimal size
此默认优先级为750.
还是很抽象,好吧,下面举个例子就明白了。
- button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button1.translatesAutoresizingMaskIntoConstraints = NO;
- [button1 setTitle:@"button 1 button 2" forState:UIControlStateNormal];
- [button1 sizeToFit];
- [self.view addSubview:button1];
- NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
- [self.view addConstraint:constraint];
- constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
- [self.view addConstraint:constraint];
- constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-150.0f];
- constraint.priority = 751.0f;
- [self.view addConstraint:constraint];
运行效果如下:
代码很容易理解,创建一个button,设置其constraint,使其距离顶部100,距离左边100,距离右边150,注意右边的constraint的优先级我们设置的为751.0,此时autolayout系统会去首先满足此constraint,再去满足Content Compression Resistance(其优先级为750,小于751)。
当我们把751变为749的时候,效果如下:
这时就是先满足了Content Compression Resistance的constraint,因为其优先级高。
下面举的例子为Content Hugging的例子,代码如下:
- button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- button1.translatesAutoresizingMaskIntoConstraints = NO;
- [button1 setTitle:@"button 1 button 2" forState:UIControlStateNormal];
- [button1 sizeToFit];
- [self.view addSubview:button1];
- NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
- [self.view addConstraint:constraint];
- constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
- [self.view addConstraint:constraint];
- constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
- constraint.priority = 251.0f;
- [self.view addConstraint:constraint];
运行效果如下:
当把优先级priority改为249的时候,效果如下:
这个就不用解释了吧。
转:AutoLayout中的Content Hugging 和 Content Compression Resistance的更多相关文章
- iOS开发 - Content hugging priority & Content compression resistance priority
1. 什么是Content hugging priority 你可以把它想象成一根放在视图上的橡皮筋. 这根橡皮筋会组织视图超过它本身的固有大小(intrinsic content size). 它存 ...
- Autolayout中Hugging和Compression使用注意
前言 本文主要侧重Autolayout使用过程中,通过代码和SB添加含有intrinsicSize属性控件约束的一些细节. 来自我的博客,欢迎访问:To Be Independent. Hugging ...
- 在代码中使用Autolayout – intrinsicContentSize和Content Hugging Priority
我们继续来看在代码中使用Autolayout的话题.先说intrinsicContentSize,也就是控件的内置大小.比如UILabel,UIButton等控件,他们都有自己的内置大小.控件的内置大 ...
- iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】
原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...
- iOS开发之AutoLayout中的Content Hugging Priority和 Content Compression Resistance Priority解析
本篇博客的内容也不算太复杂,算是AutoLayout的一些高级的用法.本篇博客我们主要通过一些示例来看一下AutoLayout中的Content Hugging Priority以及Content C ...
- AutoLayout学习之理解intrinsicContentSize,Content Hugging Priority,Content Compression Resistance Priority
TableViewCell的高度计算应该是所有开发者都会使用到的东西,之前都是用代码计算的方法来计算这个高度.最近有时间看了几个计算Cell高度的方法.基本上都用到了AutoLayout,这篇首先介绍 ...
- intrinsicContentSize和Content Hugging Priority
Mgen | 首页 | | 发新文章 | 联系 | 订阅 | 管理 iOS: 在代码中使用Autolayout (2) - intrinsicContentSize和Content Hugging ...
- Content Provider Basics ——Content Provider基础
A content provider manages access to a central repository of data. A provider is part of an Android ...
- xcode中使用xib添加autolayout中constrain to margins的不同
在使用xcode7 在storyboard中添加autolayout中发现 如果添加在view 直接添加到viewcontroller的view 上 constrain to margins 只 ...
随机推荐
- 《JavaScript》数组Array
构造函数 var arr1 = new Array();// [] 空数组 var arr2 = new Array(3);//定义长度,0是空数组 var arr3 = new Array(1,2, ...
- 《UML大战需求分析》-读后感三
用例图是用来描述什么角色通过某某系统能做什么的图,用例图关注的是系统的外在表示想爱你.系统与人的交互系统与其他系统的交互,小人执行者就是角色,角色 是对系统使用者的抽象,一个角色可以代表多个具体的人而 ...
- 浅谈对IT的认识!
我是一个从农村出来的学生,家里的情况和大多数的农村同学是一样的,家里算不上有钱,父母供我读书,也已经是做到仁至义尽了. 我现在选了,一个和计算机有关的专业---计算机应用技术.就是希望毕业后,可以找到 ...
- EJB是什么
EJB (enterprise java bean) EJB 概念的剖析 我们先看一下,EJB 的官方解释: 商务软件的核心部分是它的业务逻辑.业务逻辑抽象了整个商务过程的流程,并使用计 算机语言 ...
- python 创建目录
Python对文件的操作还算是方便的,只需要包含os模块进来,使用相关函数即可实现目录的创建. 主要涉及到三个函数 1.os.path.exists(path) 判断一个目录是否存在 2.os.mak ...
- 设置session的过期时间
1)修改php.ini文件中的gc_maxlifetime变量就可以延长session的过期时间了 session.gc_maxlifetime = 86400 然后,重启你的web服务(一般是apa ...
- spring 整合 Struts1.X [转]
这篇博客摘自[http://blog.csdn.net/chendc201/article/details/8464008], 其中也有一些是自己增加的部分 . 第一步, 需要为 Struts 装载 ...
- struts 类型转换
- MySQL复制 -- 复制出错怎么办?
假如我们生产环境复制出错?该怎么办呢? 下面提供几种办法: 1. 手工处理,补回不一致数据(可以利用主库来补数据.也可以利用binlog来补数据) 2.用开源工具来解决一致性问题 3.自己造轮子,解决 ...
- BZOJ5125 小Q的书架(决策单调性+动态规划+分治+树状数组)
设f[i][j]为前i个划成j段的最小代价,枚举上个划分点转移.容易想到这个dp有决策单调性,感性证明一下比较显然.如果用单调栈维护决策就不太能快速的求出逆序对个数了,改为使用分治,移动端点时树状数组 ...