前言

本文主要侧重Autolayout使用过程中,通过代码和SB添加含有intrinsicSize属性控件约束的一些细节。

来自我的博客,欢迎访问:To Be Independent.

Hugging和Compression 属性

有很多关于这两个概念的文章,比如stackoverflow上Cocoa Autolayout: content hugging vs content compression resistance priority。我觉得很形象的说明了设置了有什么用,但是还欠缺什么时候使用,即和`intrinsicSize`的关系。先来看下文档上的说明:

- contentCompressionResistancePriorityForAxis:
//Returns the priority with which a view resists being made smaller than its intrinsic size. - contentHuggingPriorityForAxis:
//Returns the priority with which a view resists being made larger than its intrinsic size.

这么一看,就很明了:对于有 intrinsicSize 属性的控件(如UILabel,UIButton等),如果当前的frame比显示的content范围大,那么设置的Hugging属性起作用,否则设置的Compression属性起作用。对于相应的数值,越大表明优先级越高,意味着当前的属性占优。简单的说,对于需要Hugging的情形,hugging属性的值越大(优先级越高),那么表明控件需要紧凑的显示。
Hugging和Compression属性值有默认值:

  1. 对于纯代码添加的控件,Hugging默认250.0f,Compression默认 750.0f
  2. 通过SB添加,Hugging默认251.0f,Compression默认750.0f

为什么会有不一样的值?那么先看一下 UILayoutPriority的取值:

static const UILayoutPriority UILayoutPriorityRequired NS_AVAILABLE_IOS(6_0) = ; // A required constraint.  Do not exceed this.

static const UILayoutPriority UILayoutPriorityDefaultHigh NS_AVAILABLE_IOS(6_0) = ; // This is the priority level with which a button resists compressing its content.

static const UILayoutPriority UILayoutPriorityDefaultLow NS_AVAILABLE_IOS(6_0) = ; // This is the priority level at which a button hugs its contents horizontally.

static const UILayoutPriority UILayoutPriorityFittingSizeLevel NS_AVAILABLE_IOS(6_0) = ;// When you send -[UIView systemLayoutSizeFittingSize:], the size fitting most closely to the target size (the argument) is computed.  UILayoutPriorityFittingSizeLevel is the priority level with which the view wants to conform to the target size in that computation.  It's quite low.  It is generally not appropriate to make a constraint at exactly this priority.  You want to be higher or lower.

由此可见,在设计的时候,iOS的开发人员考虑到类似UILabel的控件首要的是显示所有的内容。

编程实现

通过一段代码,加载一个button:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.translatesAutoresizingMaskIntoConstraints = NO;
button.backgroundColor = [UIColor redColor];
[button setTitle:@"a long long title" forState:UIControlStateNormal];
[self.view addSubview:button]; NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint]; constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
[self.view addConstraint:constraint]; constraint = [[NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier: constant:50.0f];
[self.view addConstraint:constraint];

如上添加的constraint,使得button的frame不足以显示标题内容,注意上述constraint默认的优先级都是UILayoutPriorityRequired。因此我们可以通过修改最后一个宽度的constraint:

constraint.priority = UILayoutPriorityDefaultHigh - ;

对于用SB添加的控件,也可以用类似的方法修改。至于为什么,SB中添加的如UILable的控件,当给其添加某个约束后,SB中Hugging属性的值是251呢?这是为了默认可以显示全内容。此时,你可以在sb中手动把空间尺寸变小,再把控件的某个属性的constriant(width或tailing)的优先级设置为low。这时,你也可以在SB中发现相应的约束由蓝色实线变成了蓝色虚线。当然,如果compression约束起作用的情况下,约束也是蓝色虚线。

与其它控件一起使用

如上单个控件可以正常使用,如果设置一个相邻的控件,会有什么需要注意的吗?答案是NO,什么都不需要操心,仍旧按之前的方法添加约束,这极大的简化了工作量。
另外,这里需要说明的是,需要更新控件上文字的时候,为了有一个较好的动画效果,需要:

[label.superView layoutIfNeeded];
 
 

Autolayout中Hugging和Compression使用注意的更多相关文章

  1. iOS开发之AutoLayout中的Content Hugging Priority和 Content Compression Resistance Priority解析

    本篇博客的内容也不算太复杂,算是AutoLayout的一些高级的用法.本篇博客我们主要通过一些示例来看一下AutoLayout中的Content Hugging Priority以及Content C ...

  2. xcode中使用xib添加autolayout中constrain to margins的不同

    在使用xcode7 在storyboard中添加autolayout中发现 如果添加在view 直接添加到viewcontroller的view 上 constrain to margins    只 ...

  3. iOS Core Animation具体解释(四)AutoLayout中的动画

    原创blog.转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn.net/column/details/huang ...

  4. 转:AutoLayout中的Content Hugging 和 Content Compression Resistance

    OS6中引入了AutoLayout,极大的方便了UI元素的布局,现在已经过去一年了,并且大部分设备的系统也已经升级到了iOS6,是时候要使用此项技术了. 在AutoLayout的学习中有两个概念官方文 ...

  5. 怎样在 AutoLayout 中使用 UIScrollView (多个ContentView)

    http://codehappily.wordpress.com/2013/11/14/ios-how-to-use-uiscrollview-with-autolayout-pure-autolay ...

  6. iOS-AutoLayout中动画使用的细节 和 iOS layout机制

    在Main.storyboard拖入一个UIView,随便设置一个背景色, 使用autolayout  为紫色的view添加约束 :(0,0,100,100) , 为该view添加动画代码如下: #i ...

  7. AutoLayout中使用UIScrollView

    UIScrollView 在 Auto Layout 是一个很特殊的 view,对于 UIScrollView 的 subview 来说,它的 leading/trailing/top/bottom ...

  8. 在代码中使用Autolayout – intrinsicContentSize和Content Hugging Priority

    我们继续来看在代码中使用Autolayout的话题.先说intrinsicContentSize,也就是控件的内置大小.比如UILabel,UIButton等控件,他们都有自己的内置大小.控件的内置大 ...

  9. iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】

    原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...

随机推荐

  1. html5 文档元素 header footer h1h2h4

    文档元素: 用于在页面的大布局, 区分各个主体和概念. 让整体清晰, 元素有语义, 进一步代替div 具体划分图示: (参考) <hgroup> <section> <n ...

  2. Linux 新手入门常用命令

    1,增加用户:useradd mylinux passwd mylinux 添加你的用户密码 2,切换用户: su otheruser   (注意这种切换方式只是临时的,本质工作目录还在原来的用户目录 ...

  3. 前端构建工具Gulp的学习和使用

    前几天刚鼓捣了Grunt的使用,结果文档还没捂热,老大说我们还是用gulp吧,搞得我又得来整gulp,眼泪流成河了,真是不晓得底层人民的辛苦啊.不过经过对gulp的学习,发现很好用,比grunt舒服! ...

  4. windows&cmd常用命令&快捷键

    1: cls即CLear Screen 功能:清除屏幕上的所有显示 2:Win+Tab快捷键进行3D窗口切换还有比较快速切换窗口的新方法. 3:利用Alt+Tab快捷键进行切换窗口时,在桌面中间会显示 ...

  5. maven(15),快照与发布,RELEASE与SNAPSHOT

     发布RELEASE 用户A将代码打包发布到RELEASE仓库,具体操作参考上篇文章.用户B使用时,需要在pom.xml添加JAR包的依赖坐标.如果用户A将版本从1.0升级为2.0,用户B使用时也 ...

  6. cache2go开源项目的回调方法使用

    https://github.com/muesli/cache2go 结构体 type CacheTable struct { sync.RWMutex . name string items map ...

  7. python基础——操作系统简介

    不同应用领域的主流操作系统 l  桌面操作系统 l  服务器操作系统 l  嵌入式操作系统 l  移动设备操作系统 桌面操作系统 Windows系列 用户群体很大 MacOS 适合于开发人员 Linu ...

  8. 五、python小功能记录——打包程序

    使用pyinstaller打包Python程序 安装工具 :pip3 install pyinstaller 在Python程序文件夹上(不点进去)按住shift并且右键,在弹出的选项中点击" ...

  9. 使用.Net访问Office编程接口(PIA和IA的区别)

    在这篇文章里面,我将向大家介绍如何在.Net中访问Office所公开的编程接口.其实,不管是使用哪种具体的技术来针对Office进行开发(比如VSTO,或者用C#编写一个Office Add-in,或 ...

  10. day13 多线程建立方法

    #01创建多线程    继承Thread类    覆盖run方法:run方法里面运行要执行的代码    创建对象    调用start方法,start方法会开启线程,然后调用run方法 获取线程名字: ...