@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. 1.Linux入门介绍

    1.1 Linux概述 1.1.1 Linux简要介绍 Linux的由来: Linux的内核最初是由芬兰人李纳斯·托瓦茨在上大学的时候编写的一个内核,它是基于Unix操作系统编写的 大多服务器使用的是 ...

  2. POJ-1655 Balancing Act(树的重心)

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  3. 理解Mysql prepare预处理语句

    MySQL 5.1对服务器一方的预制语句提供支持.如果您使用合适的客户端编程界面,则这种支持可以发挥在MySQL 4.1中实施的高效客户端/服务器二进制协议的优势.候选界面包括MySQL C API客 ...

  4. 洛谷 3391 【模板】文艺平衡树 Treap区间翻转

    [题解] 用Treap维护这个序列. 加入的时候直接插入到末尾,这样Treap就变成一棵以插入时间先后为排序关键字的二叉搜索树. 对于翻转操作,我们分裂出需要翻转的区间,给这个区间的root打一个翻转 ...

  5. lambda表达式与委托与线程初步谈论-基于刘铁锰视频观后操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. unigui的session【1】

    目前是1394. 明白session如何使用管理,看demo Session List和SessionTimeout unit Main; interface uses Windows, Messag ...

  7. Drop all tables in MySQL database

    Drop all tables in MySQL database Answer: MySQL does not have a command for removing all database ta ...

  8. bootstrap下的双选时间插件使用方法

    bootstrap画的页面很漂亮,能自动适应网页端,移动端.实现一个双选时间控件: 要得jar包自己去下 一.页面 二.JS var $createTime=$('#createTime');$cre ...

  9. 浅析gcc、arm-linux-gcc和arm-elf-gcc关系

    浅析gcc.arm-linux-gcc和arm-elf-gcc关系 一.GCC简介 The GNU Compiler Collection,通常简称 GCC,是一套由 GNU 开发的编译器集,为什么是 ...

  10. 【线段树I:母题】hdu 1166 敌兵布阵

    [线段树I:母题]hdu 1166 敌兵布阵 题目链接:hdu 1166 敌兵布阵 题目大意 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又開始忙乎了.A国在海 ...