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 ...
随机推荐
- ModelBinder 请求容错性
代码 //using System.Web.Mvc; public class TrimToDBCModelBinder : DefaultModelBinder { public override ...
- [SQL Server] 常用sql脚本
1.添加表 GO IF NOT EXISTS(SELECT * FROM sys.tables WHERE name='table_name') BEGIN CREATE TABLE [dbo].[t ...
- Google Cloud SSH 连接配置
设置当前用户及root用户的密码 sudo passwd xx-user # 输入新密码 sudo passwd root # 输入新密码(建议保持一样) 在本地生成私钥和公钥 cd ~/.ssh s ...
- BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer
[题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...
- Django——8 关系表的数据操作 表关联对象的访问 多表查询
Django 关系表中的数据操作 表关联对象的访问 关联对象的add方法 create方法 remove方法 clear方法 多表查询 查询补充 聚合查询 分组查询 F查询 Q查询 关系表的数据操作 ...
- NYIST 760 See LCS again
See LCS again时间限制:1000 ms | 内存限制:65535 KB难度:3 描述There are A, B two sequences, the number of elements ...
- 清北学堂模拟赛d1t4 一道图论好题(graph)
题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,不仅有边权还有点权. LYK给出了一个子图的定义,一张图G’={V’,E’}被称作G的子图,当且仅当 ·G ...
- paste deploy初探
这段时间刚着手开始研究Openstack Swift源码,为后续开发做准备. Swift依据python WSGI规范.WSGI(Web Server Gateway Interface)是Pytho ...
- BZOJ1193 马步距离 (贪心)
恶心的题目= = #include <cstdio> #include <cmath> #include <algorithm> ][]={{,,,,},{,,,, ...
- 洛谷—— P2733 家的范围 Home on the Range
https://www.luogu.org/problem/show?pid=2733 题目背景 农民约翰在一片边长是N (2 <= N <= 250)英里的正方形牧场上放牧他的奶牛.(因 ...