@interface ViewController ()
{ UIView *firstView;
UIView *secondView;
UIView *thirdView; }
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; /**
第一个view
*/
firstView = [[UIView alloc]init];
firstView.translatesAutoresizingMaskIntoConstraints = NO;
firstView.backgroundColor = [UIColor blueColor];
[self.view addSubview:firstView]; /**
第二个view
*/
secondView = [[UIView alloc]init];
secondView.translatesAutoresizingMaskIntoConstraints = NO;
secondView.backgroundColor = [UIColor brownColor];
[self.view addSubview:secondView]; /**
第三个view
*/
thirdView = [[UIView alloc]init];
thirdView.translatesAutoresizingMaskIntoConstraints = NO;
thirdView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:thirdView]; /**绑定三个view*/
NSDictionary *dic_bind = NSDictionaryOfVariableBindings(firstView,secondView,thirdView);
/**设置view之间的间距和高度*/
NSDictionary *dic_Constraint = @{ @"padding":@(.f),
@"height":@(.f)
}; /**
* 第一个view添加约束
*/
/**垂直方向居中对齐*/
NSLayoutConstraint *first_CenterY = [NSLayoutConstraint constraintWithItem:firstView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier: constant:]; /**垂直方向添加高度约束*/
NSArray *first_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[firstView(height)]" options: metrics:dic_Constraint views:dic_bind];
[self.view addConstraints:@[first_CenterY]];
[self.view addConstraints:first_V]; /**
* 第二个view添加约束
*/
/**垂直方向居中对齐*/
NSLayoutConstraint *second_CenterY = [NSLayoutConstraint constraintWithItem:secondView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier: constant:]; /**垂直方向添加高度约束*/
NSArray *second_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[secondView(height)]" options: metrics:dic_Constraint views:dic_bind];
[self.view addConstraint:second_CenterY];
[self.view addConstraints:second_V]; /**
* 第三个view添加约束
*/
/**垂直方向居中对齐*/
NSLayoutConstraint *third_CenterY = [NSLayoutConstraint constraintWithItem:thirdView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier: constant:]; /**垂直方向添加高度约束*/
NSArray *third_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[thirdView(height)]" options: metrics:dic_Constraint views:dic_bind];
[self.view addConstraints:@[third_CenterY]];
[self.view addConstraints:third_V]; /**给三个view添加水平约束等宽等间距*/
NSArray * allConstraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[firstView]-10-[secondView(firstView)]-padding-[thirdView(secondView)]-padding-|" options: metrics:dic_Constraint views:dic_bind];
[self.view addConstraints:allConstraint_H]; }

ios -使用NSLayoutConstraint实现多个view等宽等高等间距的更多相关文章

  1. iOS开发UI篇—控制器的View的创建

    iOS开发UI篇—控制器的View的创建 一.6种创建控制器View的方式 #import "NJAppDelegate.h" #import "NJViewContro ...

  2. iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

    iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...

  3. 【IOS笔记】Creating Custom Content View Controllers

    Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...

  4. iOS 11 scroll滚动偏移,tableview偏移44,获取view的宽和高

    1. tableview 的头部 有44的偏移量 1>.设置 tableview的 属性 tableView.scrollIndicatorInsets = UIEdgeInsets.zero ...

  5. 在渲染前获取 View 的宽高

    在渲染前获取 View 的宽高 这是一个比较有意义的问题,或者说有难度的问题,问题的背景为:有时候我们需要在view渲染前去获取其宽高,典型的情形是,我们想在onCreate.onStart.onRe ...

  6. 【转】Android 获得view的宽和高

     转自:http://blog.csdn.net/yangdeli888/article/details/25405263 Android 获得view的宽和高 分类: android 技术点项目20 ...

  7. Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?

    在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...

  8. 用addOnGlobalLayoutListener获取View的宽高

    首先,我们在onCreate方法里调用getHeight()和 getWidth()是不能正确获取View的宽高的,因为onCreate方法执行完了,我们定义的控件才会被onMeasure()度量,所 ...

  9. Android中如何在代码中设置View的宽和高?

    Android中如何在代码中设置View的宽和高?https://zhidao.baidu.com/question/536302117.htmlhttps://blog.csdn.net/u0141 ...

随机推荐

  1. Scrapy爬虫框架示意图汇总

  2. MySQL中是索引

    MySQL中是索引: --.唯一索引: 一行中的内容不能一样, create t2( id int , num int, unique weiyisuiyin (id,num) ) --唯一; --约 ...

  3. ansible - 基本用法

    目录 ansible - 01 一. 安装与使用 ansible命令格式 查看ansible生成的配置文件 ssh认证方式 ansible的第一个命令 弱口令校验 host-pattern的格式 模块 ...

  4. hdu 5175 Misaki's Kiss again

    Misaki's Kiss again  Accepts: 75  Submissions: 593  Time Limit: 2000/1000 MS (Java/Others)  Memory L ...

  5. seleniumd对象的操作方法,属性,keys

    这是通过find方法找到的页面元素,此对象提供了多种方法,让我们可以与页面元素进行交互,例如点击.清空. 目录: 1. 方法 2. 属性 3. keys 方法 clear()清空 如果当前元素中有文本 ...

  6. 【[Offer收割]编程练习赛14 D】剑刃风暴(半径为R的圆能够覆盖的平面上最多点数目模板)

    [题目链接]:http://hihocoder.com/problemset/problem/1508 [题意] [题解] 求一个半径为R的圆能够覆盖的平面上的n个点中最多的点数; O(N2log2N ...

  7. [bzoj1592][Usaco09Feb]Making the Grade 路面修整_动态规划

    Making the Grade 路面修整 bzoj-1592 题目大意:给你n段路,每段路有一个高度h[i],将h[i]修改成h[i]$\pm\delta$的代价为$\delta$,求将这n段路修成 ...

  8. N天学习一个linux命令之vmstat

    用途 查看系统资源整体使用情况,包括进程数量,CPU,内存,IO,swap等资源统计信息 用法 vmstat [options] [delay [ count]] 常用选项 -a 显示active/i ...

  9. [转]十五天精通WCF——第十一天 如何对wcf进行全程监控

    说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...

  10. 最简单的基于FFmpeg的移动端样例:Windows Phone HelloWorld

    ===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...