ios -使用NSLayoutConstraint实现多个view等宽等高等间距
@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等宽等高等间距的更多相关文章
- iOS开发UI篇—控制器的View的创建
iOS开发UI篇—控制器的View的创建 一.6种创建控制器View的方式 #import "NJAppDelegate.h" #import "NJViewContro ...
- iOS开发UI篇—使用picker View控件完成一个简单的选餐应用
iOS开发UI篇—使用picker View控件完成一个简单的选餐应用 一.实现效果 说明:点击随机按钮,能够自动选取,下方数据自动刷新. 二.实现思路 1.picker view的有默认高度为162 ...
- 【IOS笔记】Creating Custom Content View Controllers
Creating Custom Content View Controllers 自定义内容视图控制器 Custom content view controllers are the heart of ...
- iOS 11 scroll滚动偏移,tableview偏移44,获取view的宽和高
1. tableview 的头部 有44的偏移量 1>.设置 tableview的 属性 tableView.scrollIndicatorInsets = UIEdgeInsets.zero ...
- 在渲染前获取 View 的宽高
在渲染前获取 View 的宽高 这是一个比较有意义的问题,或者说有难度的问题,问题的背景为:有时候我们需要在view渲染前去获取其宽高,典型的情形是,我们想在onCreate.onStart.onRe ...
- 【转】Android 获得view的宽和高
转自:http://blog.csdn.net/yangdeli888/article/details/25405263 Android 获得view的宽和高 分类: android 技术点项目20 ...
- Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?
在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...
- 用addOnGlobalLayoutListener获取View的宽高
首先,我们在onCreate方法里调用getHeight()和 getWidth()是不能正确获取View的宽高的,因为onCreate方法执行完了,我们定义的控件才会被onMeasure()度量,所 ...
- Android中如何在代码中设置View的宽和高?
Android中如何在代码中设置View的宽和高?https://zhidao.baidu.com/question/536302117.htmlhttps://blog.csdn.net/u0141 ...
随机推荐
- Django - 模版语言循环字典
1.可以对传入字典参数做循环显示 views.py中代码: urls.py中代码: html中代码: 在模版语言中,可以对字典进行类似python中的操作(keys,values,items),需要注 ...
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- Cent os常见操作命令
1.查看防火墙状态:firewall-cmd –-state 2.关闭防火墙:systemctl stop firewalld.service 3.禁止防火墙开机启动:systemctl disabl ...
- Python基础-奇偶判断调用函数
编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数 1/1+1/3+...+1/n. 首先写一个n为偶数的函数: def peven(n): s = 0 ...
- NOD 1113矩阵快速幂
基准时间限制:3 秒 空间限制:131072 KB 分值: 40 给出一个N * N的矩阵,其中的元素均为正整数.求这个矩阵的M次方.由于M次方的计算结果太大,只需要输出每个元素Mod (10^ ...
- 303. Range Sum Query - Immutable(动态规划)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- bug的分类和等级
一.bug的定义 软件的bug,狭义指软件程序的漏洞或缺陷,广义指测试工程师或用户提出的软件可改进的细节.或与需求文档存在差异的功能实现等 对应三个测试目的:(3个为了) 1.为了发现程序的代码或业务 ...
- everyday two problems / 3.11 - 3.17
7日共14题,因为3.14两题相同,所以实际总共13题 13道题分为难易两部分,没有按照时间顺序排列题目 A.易: 1.覆盖数字的数量 题意: 给出一段从A - B的区间S(A,B为整数) 这段区间内 ...
- 动态加入的HTML的自己主动渲染
这两天在写一个用EasyUI的前台,遇到动态向Layout加入HTML内容时没有自己主动渲染的问题.查了一下网上的资料后得以解决.详细例如以下: $("#content").htm ...
- GPS-Graph Processing System 改动源代码经验总结 (四)
HamaWhite原创,转载请注明出处.欢迎大家增加Giraph 技术交流群: 228591158 本文目的:在改动GPS源代码后,具体描写叙述怎样编译和分发到各Worker节点上. 以下以Graph ...