1、NSLayoutConstraint简介

  适配界面大多用Masonry工具,也是基于NSLayoutConstraint写的!通过使用两个类方法实现自动布局:

+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts metrics:(nullable NSDictionary<NSString *,id> *)metrics
views:(NSDictionary<NSString *, id> *)views; +(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(nullable id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;

  1》使用自动布局之前设置view的自动布局约束为NO(view.translatesAutoresizingMaskIntoConstraints = NO)

  2》UIViewController有个方法- (void)updateViewConstraints;

    UIView有个方法- (void)updateConstraints;在对应的方法中设置自动布局;

2、如何使用constraintsWithVisualFormat和VFL语言

  2.1》constraintsWithVisualFormat方法说明:

+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format
options:(NSLayoutFormatOptions)opts
metrics:(nullable NSDictionary<NSString *,id> *)metrics
views:(NSDictionary<NSString *, id> *)views;

  方法说明:此方法通过VFL语言进行适配

  format:VFL语句字符串形式
  opts:枚举参数 NSLayoutFormatOptions
  metrics:字典 放置VFL语言用到的参数(例如height)对应key
  views:字典 放置VFL语言用到的view对应key

  2.2》VFL语言简介

    VFL(Visual Format Language)可视化格式语言,是苹果公司为了简化AutoLayout而编码推出的抽象语言。

具体表示方法:
水平方向 H:    垂直方向 V:    Views [view]
关系    >=,==,<= SuperView |    空间,间隙- -
优先级    @value

  2.3》简单使用

    创建一个蓝色view,离父视图上左右距离为20px、高为20:

    UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
NSArray *arr = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[blueView]-|"
options:
metrics:nil
views:NSDictionaryOfVariableBindings(blueView)];
NSArray *arrV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[blueView(==height)]"
options:
metrics:@{@"height":@""}
views:NSDictionaryOfVariableBindings(blueView)];
[self.view addConstraints:arr];
[self.view addConstraints:arrV];

    在蓝色view下方创建一个黄色view,左右距离父视图40px、上边离蓝色view8px、高度为20px:

    UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
yellowView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:yellowView];
NSArray *arr2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[yellowView]-40-|"
options:
metrics:nil
views:NSDictionaryOfVariableBindings(yellowView)];
NSArray *arr2V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[blueView]-[yellowView(==height)]"
options:
metrics:@{@"height":@""}
views:NSDictionaryOfVariableBindings(blueView,yellowView)];
[self.view addConstraints:arr2];
[self.view addConstraints:arr2V];

    注意:-:在父视图中表示20px,同级视图表示8px;

       参数metrics和views要和VFL语言的参数对应,否则会崩溃;  

3、如何使用constraintWithItem方法

  3.1》constraintWithItem方法说明:

+(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1
relatedBy:(NSLayoutRelation)relation
toItem:(nullable id)view2
attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier
constant:(CGFloat)c;
view1:设置的视图
attr1:view1设置的属性
relation:视图view1和view2的属性关系
view2:参照视图
attr2:view2设置的属性
multiplier:视图view1指定属性是view2指定属性的多少倍
c:view1指定属性需要添加的浮点数

  3.2》简单使用

    添加一个view位于父视图中心、宽度相等、高度为父视图一半:

    UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:blueView];
NSMutableArray *constraints = [NSMutableArray array];
[constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:. constant:0.0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[constraints addObject:[NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.view addConstraints:constraints];

4、NSLayoutRelation、NSLayoutAttribute、NSLayoutFormatOptions枚举说明

  4.1》NSLayoutRelation说明:

typedef NS_ENUM(NSInteger, NSLayoutRelation) {
NSLayoutRelationLessThanOrEqual = -, //视图关系小于或等于
NSLayoutRelationEqual = ,//视图关系等于
NSLayoutRelationGreaterThanOrEqual = ,//视图关系大于或等于
};

  4.2》NSLayoutAttribute说明:

typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = ,//视图的左边
NSLayoutAttributeRight,//视图的右边
NSLayoutAttributeTop,//视图的上边
NSLayoutAttributeBottom,//视图的下边
NSLayoutAttributeLeading,//视图的前边
NSLayoutAttributeTrailing,//视图的后边
NSLayoutAttributeWidth,//视图的宽度
NSLayoutAttributeHeight,//视图的高度
NSLayoutAttributeCenterX,//视图中心的X值
NSLayoutAttributeCenterY,//视图中心的Y值
NSLayoutAttributeLastBaseline,//视图的下基准线
NSLayoutAttributeBaseline NS_SWIFT_UNAVAILABLE("Use 'lastBaseline' instead") = NSLayoutAttributeLastBaseline,
NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),//视图的上基准线
NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),//视图的左边距
NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),//视图的右边距
NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),//视图的上边距
NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),//视图的下边距
NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),//视图的前边距
NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),//视图的后边距
NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),//视图的中心X边距
NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),//视图的中心Y边距
NSLayoutAttributeNotAnAttribute = //无属性
};

  注意:NSLayoutAttributeLeading和NSLayoutAttributeTrailing表示前、后边,因为中国书写是从左向右。如果从右向左则表示后、前边;

       NSLayoutAttributeLastBaseline和NSLayoutAttributeFirstBaseline表示基准线,举例UILabel的上下基准线就是文字的上下边线;

  4.3》NSLayoutFormatOptions说明

typedef NS_OPTIONS(NSUInteger, NSLayoutFormatOptions) {
NSLayoutFormatAlignAllLeft = ( << NSLayoutAttributeLeft),//所有视图左边缘对齐。
NSLayoutFormatAlignAllRight = ( << NSLayoutAttributeRight),//所有视图右边缘对齐
NSLayoutFormatAlignAllTop = ( << NSLayoutAttributeTop),//所有视图顶部对齐
NSLayoutFormatAlignAllBottom = ( << NSLayoutAttributeBottom),//所有视图底部对齐
NSLayoutFormatAlignAllLeading = ( << NSLayoutAttributeLeading),//视图当前区域文字开始的边缘对齐(英文:左边,希伯来语:右边)
NSLayoutFormatAlignAllTrailing = ( << NSLayoutAttributeTrailing),//视图当前区域文字结束的边缘对齐(英文:右边,希伯来语:左边)
NSLayoutFormatAlignAllCenterX = ( << NSLayoutAttributeCenterX),//视图中心点x对齐
NSLayoutFormatAlignAllCenterY = ( << NSLayoutAttributeCenterY),//视图中心点y对齐
NSLayoutFormatAlignAllLastBaseline = ( << NSLayoutAttributeLastBaseline),//视图内容底部基线对齐、有文字就是文字底部
NSLayoutFormatAlignAllBaseline NS_SWIFT_UNAVAILABLE("Use 'alignAllLastBaseline' instead") = NSLayoutFormatAlignAllLastBaseline,
NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = ( << NSLayoutAttributeFirstBaseline),,//视图内容定部基线对齐、有文字就是文字顶部
//根据编辑内容和编写方向方式进行对齐
NSLayoutFormatAlignmentMask = 0xFFFF,
NSLayoutFormatDirectionLeadingToTrailing = << , // default
NSLayoutFormatDirectionLeftToRight = << ,
NSLayoutFormatDirectionRightToLeft = << ,
NSLayoutFormatDirectionMask = 0x3 << ,
NSLayoutFormatSpacingEdgeToEdge API_AVAILABLE(ios(11.0),tvos(11.0)) = << , // default
NSLayoutFormatSpacingBaselineToBaseline API_AVAILABLE(ios(11.0),tvos(11.0)) = << ,
NSLayoutFormatSpacingMask API_AVAILABLE(ios(11.0),tvos(11.0)) = 0x1 << ,
};

iOS开发NSLayoutConstraint代码自动布局的更多相关文章

  1. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  2. iOS开发 纯代码创建UICollectionView

    转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xi ...

  3. iOS 开发 – 均衡代码职责

    前言 文章的标题有点绕口,不过想了半天,想不到更好的标题了.本文的诞生有一部分功劳要归于iOS应用现状分析,标题也是来源于原文中的"能把代码职责均衡的划分到不同的功能类里".如果你 ...

  4. 设计模式之单一职责原则(iOS开发,代码用Objective-C展示)

    单一职责原则:就一个类而言,应该只有一个引起它变化的原因. 在iOS开发中,我们会很自然的给一个类添加各种各样的功能,比如随便写一个简单的应用程序,一般都会生成一个viewController类,于是 ...

  5. iOS开发:代码通用性以及其规范 第二篇(猜想iOS中实现TableView内部设计思路(附代码),以类似的思想实现一个通用的进度条)

    在iOS开发中,经常是要用到UITableView的,我曾经思考过这样一个问题,为什么任何种类的model放到TableView和所需的cell里面,都可以正常显示?而我自己写的很多view却只是能放 ...

  6. iOS开发:代码通用性以及其规范 第一篇(附带,自定义UITextView\进度条\双表显示\瀑布流 代码设计思路)

    在iOS团队开发中,我见过一些人的代码,也修改过他们的代码.有的人的代码写的非常之规范.通用,几乎不用交流,就可以知道如何修改以及在它基础上扩展延生.有的人的代码写的很垃圾,一眼看过去,简直会怀疑自己 ...

  7. IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载

    http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...

  8. IOS开发之----代码块的使用(二)

    iOS4引入了一个新特性,支持代码块的使用,这将从根本上改变你的编程方式.代码块是对C语言的一个扩展,因此在Objective-C中完全支持.如果你学过Ruby,Python或Lisp编程语言,那么你 ...

  9. iOS开发常用代码块(第二弹)

    GCD定时器 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ); dispat ...

随机推荐

  1. [转]C# 动态调用 WebService

    通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...

  2. 【归档】Mysql大表归档

    作为一个企业或者DBA,我们通常会有这种想法,数据是一个公司的核心命脉,应该需要永久保存,很多时候DBA和开发沟通的时候,开发人员也会这么告诉我们,这份数据非常重要,数据需要永久保存.然而,如果将数据 ...

  3. 思维题+贪心——牛客多校第一场C

    /* 给定一组n维向量 A=(a1/m,a2/m,a3/m ... an/m), 求另一个n维向量 P=(p1,p2,p3...pn),满足sum{pi}=1,使得ans=sum{(ai/m-pi)^ ...

  4. NOIp2018集训test-9-23

    这个NOI模拟题怕是比你们的NOIp模拟题要简单哦.. 友好的生物 应该是一道简单题,但是机房只有辉神一个人想到正解似乎. 被我kd-tree水过去了(这不是kd-tree的裸题吗???(不是)) / ...

  5. hdu多校第十场 1003 (hdu6693) Valentine's Day 贪心/概率

    题意: 有许多物品,每个物品有一定概率让女朋友开心.你想让女朋友开心且只开心一次,让你挑一些物品,使得这个只开心一次的概率最大,求最大概率. 题解: 设物品i让女朋友开心的概率为$p_i$ 若你挑选了 ...

  6. hdu多校第九场 1005 (hdu6684) Rikka with Game 博弈

    题意: 给一个小写字母组成的字符串,每回合轮到某人时,此人可以选择让某位+1(如果是z则变回a),或者直接结束游戏. 先手希望游戏结束时字符串字典序尽量小,后手希望游戏结束时字符串字典序尽量大,求游戏 ...

  7. c# 将byte数组保存成图片

    将byte数组保存成图片: 方式一:System.IO.File.WriteAllBytes(@"c:\test.jpg", bytes); 方式二:MemoryStream ms ...

  8. Centos6.5安装rar5.3

    linux下使用最多的压缩工具是gzip,zip等,如果需要使用rar,就必须编译安装了,以下是编译安装rar教程: 一.安装支持库yum install -y gcc gcc-c++ autocon ...

  9. 基于Netty的RPC架构学习笔记(二):netty服务器

    文章目录 简介 Netty服务端Hello World案例 举个

  10. Java 网络编程(1):使用 NetworkInterface 获得本机在局域网内的 IP 地址

    原文地址:https://segmentfault.com/a/1190000007462741 1.问题提出 在使用 Java 开发网络程序时,有时候我们需要知道本机在局域网中的 IP 地址.很常见 ...