继续Auto Layout - BNR篇。

打开BNRDetailViewController.m文件,重载viewDidLoad方法来创建UIImageView对象。当你想要给通过加载NIB文件创建的视图层级添加约束时,需要重载viewDidLoad方法。如下:

 - (void)viewDidLoad {
[super viewDidLoad]; UIImageView *iv = [[UIImageView alloc] initWithImage:nil];
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:iv];
self.imageVeiw = iv;
}

  每个视图都有一个 translatesAutoresizingMaskIntoConstraints 属性,默认为YES,即iOS会自动创建一些约束来匹配视图自动调整。

  Apple推荐使用Visual Format Language(VFL)来程序化创建约束。


VFL:一种用字符串常量来描述约束的方式。

   @"H:|-0-[imageView|-0-" ,其中H:指该约束是针对水平位置的。方括号内的imageView指要约束的视图。符号|表示视图的容器。此字符串表示:imageView离其容器左右边缘的距离为0点。同时,可简化为: @"H:|[imageView|"

   @"V:[dateLabel]-8-[imageView]-8-[tooBar]" ,其指imageView的顶部离dateLabel的距离为8点,imageView的底部离tooBar的距离为8点。可简化为: @"V:[dateLabel]-[imageView]-[tooBar]"

   @"V:[someView(==50)]" ,指视图的高度约束为50点。


  一个约束即NSLayoutConstraint类的对象,可将其添加到一个视图中。

修改viewDidLoad方法,创建imageView水平和垂直方向的约束。如下:

 - (void)viewDidLoad {
[super viewDidLoad]; UIImageView *iv = [[UIImageView alloc] initWithImage:nil];
iv.contentMode = UIViewContentModeScaleAspectFill;
iv.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:iv];
self.imageVeiw = iv; NSDictionary *nameMap = @{@"imageVeiw":self.imageVeiw, @"dateLabel":self.dateLabel, @"toolBar":self.toolBar};
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageVeiw]-0-|"
options:
metrics:nil
views:nameMap];
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[dateLabel]-[imageVeiw]-[toolBar]"
options:
metrics:nil
views:nameMap];
}

NSLayoutConstraint的类方法constraintsWithVisualFormat:options:metrics:views的第四个参数为:视觉格式化字符串中的名字与视图层级中的名字的映射。


添加约束的规则:

1)如果一个约束影响了两个具有相同父视图的view,则约束应添加到父视图中。

2)如果一个约束只影响到一个视图,该约束应添加到其所影响的视图中。

3)如果一个约束影响了两个不具有相同父视图的view,但共享一个ancestor,则该ancestor获得该约束。

4)如果一个约束影响一个视图和其父视图,则该约束添加到父视图中。

  因此,在viewDidLoad方法中,将创建的约束添加到父视图中,添加如下粗体代码:

     NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[dateLabel]-[imageView]-[toolBar]"
options:
metrics:nil
views:nameMap];
[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraints];

  Intrinsic Content Size:指一个视图的大小基于其要显示的内容。

contentCompressionResistancePriority 当其值小于1000时,Auto Layout可能压缩视图。

contentHuggingPriority 当其值小于1000时,Auto Layout可能增加视图的大小。

viewDidLoad方法添加代码如下:

     ......
self.imageVeiw = iv; [self.imageVeiw setContentHuggingPriority: forAxis:UILayoutConstraintAxisVertical];
[self.imageVeiw setContentCompressionResistancePriority: forAxis:UILayoutConstraintAxisVertical];
......

创建基于比率的约束时,不能使用VFL,此时需要用到NSLayoutConstraint类的 constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: 类方法。其中multiplier属性是创建基于比率约束的关键。

 NSLayoutConstraint *aspectConstraint = [NSLayoutConstraint constraintWithItem:self.imageVeiw
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.imageVeiw
attribute:NSLayoutAttributeHeight
multiplier:1.5
constant:0.0];

添加的约束相当于 imageVeiw.width = 1.5 * imageVeiw.height + 0.0 。


  autoresizing masks:约束一个视图和其父视图的关系,但不会影响同级视图间的关系。默认情况下,会基于autoresizing masks,给视图创建和添加约束。但其经常会与用程序代码添加的约束发生冲突。此时,只需将视图的 translatesAutoresizingMaskIntoConstraints 属性设为NO即可。

Auto Layout: Programmatic Constraints - BNR的更多相关文章

  1. iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制

    iOS Programming  Auto Layout: Programmatic Constraints  1.  However, if your views are created in co ...

  2. Auto Layout Guide----(二)-----Auto Layout Without Constraints

    Auto Layout Without Constraints 没有约束的自动布局 Stack views provide an easy way to leverage the power of A ...

  3. Auto Layout Guide----(一)-----Understanding Auto Layout

    Understanding Auto Layout 理解自动布局 Auto Layout dynamically calculates the size and position of all the ...

  4. Auto Layout Guide----(三)-----Anatomy of a Constraint

    Anatomy of a Constraint 剖析约束 The layout of your view hierarchy is defined as a series of linear equa ...

  5. Auto Layout - BNR

    继续UIImageView - BNR篇. 通过Homepwner TARGETS -> General -> Deployment Info -> Devices中的iPhone改 ...

  6. iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束

    http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式的前世今生 ...

  7. 手写代码自动实现自动布局,即Auto Layout的使用

    手写代码自动实现自动布局,即Auto Layout的使用,有需要的朋友可以参考下. 这里要注意几点: 对子视图的约束,若是基于父视图,要通过父视图去添加约束. 对子视图进行自动布局调整,首先对UIVi ...

  8. Auto Layout

    Auto Layout XCode5+ Auto Layout Concepts 核心的概念是约束. Constraint Basics Constant value Relation Priorit ...

  9. 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】

    本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...

随机推荐

  1. 15分钟在笔记本上搭建 Kubernetes + Istio开发环境

    11月13~15日,KubeCon 上海大会召开,云原生是这个秋天最火热的技术.很多同学来问如何上手 Kubernetes和Istio 服务网格开发.本文将帮助你利用Docker CE桌面版,15分钟 ...

  2. Linux vi常用命令

    vi常用命令[Ctrl] + [f] 屏幕『向前』移动一页(常用)[Ctrl] + [b] 屏幕『向后』移动一页(常用)0 这是数字『 0 』:移动到这一行的最前面字符处(常用)$ 移动到这一行的最后 ...

  3. 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU启动那些事(3)- Serial Downloader模式(sdphost/MfgTool)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RT系列MCU的Serial Downloader模式. 在上一篇文章 Boot配置(BOOT Pin, eFUSE) ...

  4. [C#] 使用 StackExchange.Redis 封装属于自己的 RedisHelper

    使用 StackExchange.Redis 封装属于自己的 RedisHelper 目录 核心类 ConnectionMultiplexer 字符串(String) 哈希(Hash) 列表(List ...

  5. Android安全–加强版Smali Log注入

    有的时候我们需要注入smali调用Log输出,打印字符串的值. 比如说: 如果我们要打印下面v1的值. new-instance v1, Ljava/lang/String; const-string ...

  6. 开源IM项目-InChat登录接口设计与实现(基于Netty)

  7. audio currentTime 时间戳转换(es6语法)

    function format(interval){ if (!value) return '' let interval = Math.floor(value) let minute = (Math ...

  8. splay详解(一)

    前言 Spaly是基于二叉查找树实现的, 什么是二叉查找树呢?就是一棵树呗:joy: ,但是这棵树满足性质—一个节点的左孩子一定比它小,右孩子一定比它大 比如说 这就是一棵最基本二叉查找树 对于每次插 ...

  9. H5的语义化标签(PS: 后续继续补充)

    头部信息 <header></header> 区块标签 <figure> <figcaption>123</figcaption> < ...

  10. java StringBuilder 和 StringBuffer

    1, 相对于 String 来说, StringBuilder 和 StringBuffer 均是可变的 2, StringBuilder 线程不安全, StringBuffer 线程安全 3, 运行 ...