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 ...
随机推荐
- [二十一]JavaIO之BufferedReader 与 BufferedWriter
功能简介 BufferedReader 从字符输入流中读取文本,内部缓冲各个字符,从而实现字符.数组和行的高效读取 BufferedWriter 将文本写入字符输出流,内部缓冲各个字符,从而提供单个 ...
- springboot情操陶冶-初识springboot
前言:springboot由于其轻便和去配置化等的特性已经被广泛应用,基于时代潮流以及不被鄙视,笔者于是开辟此篇开始认识springboot 前话 springboot是基于spring而开发的轻量级 ...
- App阅读pdf和扫描二维码功能
在之前开发的Android手机App中,需要实现阅读pdf和扫描二维码的功能,在github 上找到大牛封装好包,亲测可用. 阅读pdf: https://github.com/barteksc/An ...
- 使用NOPI写入Excel基础代码
using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.IO; using Sy ...
- .NET CORE 使用Session报错:Session has not been configured for this application or request
报错信息:Session has not been configured for this application or request 解决方案:在Startup.cs文件中的Configure方法 ...
- 浅谈CLR CTS CLS。。。
So The First Is CLR CLR的全程是Common Language Runtime 公共语言运行时 可以把他理解为包含运行.net程序的引擎 和 一堆符合公共语言基础(CLI)的类 ...
- 记一次servlet项目启动
前言 tomcat 和 jetty 都属于 web 容器. mac安装tomcat brew install tomcat 安装之后,输入 catalina -h,可以看到各种命令,如run.star ...
- 教我徒弟Android开发入门(四)
本期知识点: 两大常用布局的简单介绍 在我们的APP使用第三方库 Android Studio常用快捷键 一.两大常用布局 1.LinearLayout线性布局 线性布局,可以垂直显示或者水平显示,设 ...
- Django学习之三:django-admin 和 项目manage.py程序
目录 Django django-admin 和 项目manage.py程序 它俩是什么? 命令程序用法 常用命令 Django django-admin 和 项目manage.py程序 它俩是什么? ...
- Dynamics 365的审核日志分区删除超时报错怎么办?
摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复296或者20190112可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me ...