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

在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.

还是很抽象,好吧,下面举个例子就明白了。

  1. button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  2. button1.translatesAutoresizingMaskIntoConstraints = NO;
  3. [button1 setTitle:@"button 1 button 2" forState:UIControlStateNormal];
  4. [button1 sizeToFit];
  5. [self.view addSubview:button1];
  6. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
  7. [self.view addConstraint:constraint];
  8. constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
  9. [self.view addConstraint:constraint];
  10. constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-150.0f];
  11. constraint.priority = 751.0f;
  12. [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的例子,代码如下:

  1. button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  2. button1.translatesAutoresizingMaskIntoConstraints = NO;
  3. [button1 setTitle:@"button 1 button 2" forState:UIControlStateNormal];
  4. [button1 sizeToFit];
  5. [self.view addSubview:button1];
  6. NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f];
  7. [self.view addConstraint:constraint];
  8. constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:100.0f];
  9. [self.view addConstraint:constraint];
  10. constraint = [NSLayoutConstraint constraintWithItem:button1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
  11. constraint.priority = 251.0f;
  12. [self.view addConstraint:constraint];

运行效果如下:

当把优先级priority改为249的时候,效果如下:

这个就不用解释了吧。

转:AutoLayout中的Content Hugging 和 Content Compression Resistance的更多相关文章

  1. iOS开发 - Content hugging priority & Content compression resistance priority

    1. 什么是Content hugging priority 你可以把它想象成一根放在视图上的橡皮筋. 这根橡皮筋会组织视图超过它本身的固有大小(intrinsic content size). 它存 ...

  2. Autolayout中Hugging和Compression使用注意

    前言 本文主要侧重Autolayout使用过程中,通过代码和SB添加含有intrinsicSize属性控件约束的一些细节. 来自我的博客,欢迎访问:To Be Independent. Hugging ...

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

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

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

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

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

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

  6. AutoLayout学习之理解intrinsicContentSize,Content Hugging Priority,Content Compression Resistance Priority

    TableViewCell的高度计算应该是所有开发者都会使用到的东西,之前都是用代码计算的方法来计算这个高度.最近有时间看了几个计算Cell高度的方法.基本上都用到了AutoLayout,这篇首先介绍 ...

  7. intrinsicContentSize和Content Hugging Priority

    Mgen | 首页 | | 发新文章 | 联系 | 订阅  | 管理 iOS: 在代码中使用Autolayout (2) - intrinsicContentSize和Content Hugging ...

  8. Content Provider Basics ——Content Provider基础

    A content provider manages access to a central repository of data. A provider is part of an Android ...

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

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

随机推荐

  1. 第五周作业总结(内含用Junit测试ArrayStack和LinkedStack课堂练习报告)

    ---恢复内容开始--- 学号 20162310<程序设计与数据结构>第五周学习总结 教材学习内容总结 集合分为线性集合(集合中的元素排成一行)和非线性集合(按不同于一行的方式来组织元素, ...

  2. c# 写文件注意问题及用例展示

    以txt写string举例,正确代码如下: private void xie() { FileStream fs = new FileStream("1.txt", FileMod ...

  3. sql索引的填充因子多少最好,填充因子有什么用

    和索引重建最相关的是填充因子.当创建一个新索引,或重建一个存在的索引时,你可以指定一个填充因子,它是在索引创建时索引里的数据页被填充的数量.填充因子设置为100意味着每个索引页100%填满,50%意味 ...

  4. 【Coursera】经验风险最小化

    一.经验风险最小化 1.有限假设类情形 对于Chernoff bound 不等式,最直观的解释就是利用高斯分布的图象.而且这个结论和中心极限定律没有关系,当m为任意值时Chernoff bound均成 ...

  5. Android-TCP编程

    以下是PC端代码: package com.example.sxb.myapplication;import java.io.BufferedReader;import java.io.IOExcep ...

  6. iOS- 什么是GitHub?关于它的自我介绍「初识 GitHub」

    1 前言 我一直认为 GitHub 是程序员必备技能,程序员应该没有不知道 GitHub 的才对,我当初接触 GitHub 也大概工作了一年多才开始学习使用,我读者里很多是初学者,而且还有很多是在校大 ...

  7. 51单片机RAM 数据存储区学习笔记

    转自:http://www.eepw.com.cn/article/216237_2.htm 1.RAM keil C语言编程 RAM是程序运行中存放随机变量的数据空间.在keil中编写程序,如果当前 ...

  8. java synchronized关键字浅析

    synchronized这个关键字想必学Java的人都应该知道. 直接上例子: 方法级别实例 public class AtomicInteger { private int index; publi ...

  9. 1105 C程序的推导过程

  10. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...