Auto Layout: Programmatic Constraints - 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的更多相关文章
- iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制
iOS Programming Auto Layout: Programmatic Constraints 1. However, if your views are created in co ...
- Auto Layout Guide----(二)-----Auto Layout Without Constraints
Auto Layout Without Constraints 没有约束的自动布局 Stack views provide an easy way to leverage the power of A ...
- Auto Layout Guide----(一)-----Understanding Auto Layout
Understanding Auto Layout 理解自动布局 Auto Layout dynamically calculates the size and position of all the ...
- 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 ...
- Auto Layout - BNR
继续UIImageView - BNR篇. 通过Homepwner TARGETS -> General -> Deployment Info -> Devices中的iPhone改 ...
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
http://blog.csdn.net/pucker/article/details/41843511 上一篇文章<iOS 8界面自动布局系列-1>简要介绍了iOS界面布局方式的前世今生 ...
- 手写代码自动实现自动布局,即Auto Layout的使用
手写代码自动实现自动布局,即Auto Layout的使用,有需要的朋友可以参考下. 这里要注意几点: 对子视图的约束,若是基于父视图,要通过父视图去添加约束. 对子视图进行自动布局调整,首先对UIVi ...
- Auto Layout
Auto Layout XCode5+ Auto Layout Concepts 核心的概念是约束. Constraint Basics Constant value Relation Priorit ...
- 使用Auto Layout中的VFL(Visual format language)--代码实现自动布局【转】
本文将通过简单的UI来说明如何用VFL来实现自动布局.在自动布局的时候避免不了使用代码来加以优化以及根据内容来实现不同的UI. 一:API介绍 NSLayoutConstraint API 1 2 3 ...
随机推荐
- 痞子衡嵌入式:飞思卡尔i.MX RT系列MCU启动那些事(13)- 从Serial(1-bit SPI) EEPROM/NOR恢复启动
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RT系列MCU的Serial EEPROM/NOR恢复启动. 在前几篇里痞子衡介绍的Boot Device都属于主动启 ...
- QSS的使用(二)——实现ColorLabel
在上一篇文章中,我们已经了解了QSS的基础使用,现在我们将会看到一个简单的例子来加深对QSS的理解. 需求分析 我们想要在界面中让文本显示出指定的颜色,现在有几种方案: 使用paintEvent手动计 ...
- Android安全——加固原理
一.前言 今天来介绍一下Android中的如何对Apk进行加固的原理.现阶段.我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk,结果被人反编译了,那心情真心不舒服.虽 ...
- 阿里巴巴(alibaba)系列_druid 数据库连接池_监控(一篇搞定)记录执行慢的sql语句
参考帖子:http://www.cnblogs.com/han-1034683568/p/6730869.html Druid数据连接池简介 Druid是Java语言中最好的数据库连接池.Druid能 ...
- Asp.Net MVC 读取json文件
有些系统上面的配置可以做成config里面的appsetting.这里要求写在json文件里面. 首先 添加命名空间 using Newtonsoft.Json; using System.IO; u ...
- Django 笔记分享
Django是一个基于MVC构造的框架.但是在Django中,控制器接受用户输入的部分由框架自行处理,所以 Django 里更关注的是模型(Model).模板(Template)和视图(Views), ...
- ArcGIS API for JavaScript:Layer之间那点儿事
先来看一个模型: |–TiledMapServiceLayer | |–ArcGISTiledMapServiceLayer |–DynamicLayer | |–Dyn ...
- OPC协议解析-OPC客户端与服务器通讯解析
1 OPC服务器 OPC服务器, 是指按照OPC基金组织规定的OPC规范群开发的软件驱动.OPC服务器作为中间媒介负责从数据源读取数据再跟另外一端的客户端通信.在 OPC客户端/服务器 的结 ...
- Vue双向绑定原理,教你一步一步实现双向绑定
当今前端天下以 Angular.React.vue 三足鼎立的局面,你不选择一个阵营基本上无法立足于前端,甚至是两个或者三个阵营都要选择,大势所趋. 所以我们要时刻保持好奇心,拥抱变化,只有在不断的变 ...
- reStructuredText文件语法简单学习
reStructuredText 是一种扩展名为.rst的纯文本文件,通过特定的解释器,能够将文本中的内容输出为特定的格式 1. 章节标题 章节头部由下线(也可有上线)和包含标点的标题组合创建,其中下 ...