IOS NSLayoutConstraint 页面布局(通过代码添加约束)
#import "ViewController.h" @interface ViewController ()
@property (nonatomic, strong) UIView *blueView;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 2.添加两个控件到父控件上
// 2.1添加蓝色View
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
// blueView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:blueView];
self.blueView = blueView; // 2.1添加红色View
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView]; // 1.禁用auturezing
#warning 注意, 设置父控件无效
// self.view.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
redView.translatesAutoresizingMaskIntoConstraints = NO; // 3.添加约束
// 3.1添加蓝色VIew距离父控件左边的距离固定为20 X /*
Item == first item 需要设置约束的控件
attribute == 需要设置的约束
relatedBy == relation 等于
toItem == second item 被参照的控件
attribute == 需要设置的约束
multiplier == multiplier 乘以
constant = constant 加上
*/ NSLayoutConstraint *leftCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:];
[self.view addConstraint:leftCos]; // 3.2添加蓝色VIew距离父控件右边的距离固定为20 宽度
NSLayoutConstraint *rightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-];
[self.view addConstraint:rightCos]; // 3.3添加蓝色VIew距离父控件顶部边的距离固定为20 Y
NSLayoutConstraint *topCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:];
[self.view addConstraint:topCos]; // 3.4添加蓝色View的高度 50 高
NSLayoutConstraint *heightCos = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:];
[blueView addConstraint:heightCos]; // 4.设置红色约束
// 红色的高度和蓝色高度一样 高度
NSLayoutConstraint *redHeightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:];
[self.view addConstraint:redHeightCos]; // 红色的右边和蓝色的右边对齐 X
NSLayoutConstraint *redRightCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:];
[self.view addConstraint:redRightCos]; // 红色的顶部和蓝色的底部距离固定 Y
NSLayoutConstraint *redTopCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:];
[self.view addConstraint:redTopCos]; // 红色的宽度等于蓝色宽度的一半 宽度
NSLayoutConstraint *redwidthCos = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:];
[self.view addConstraint:redwidthCos]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@", NSStringFromCGRect(self.blueView.frame));
} @end
IOS NSLayoutConstraint 页面布局(通过代码添加约束)的更多相关文章
- 【转】iOS学习之Autolayout(代码添加约束) -- 不错不错
原文网址:http://www.cnblogs.com/HypeCheng/articles/4192154.html DECEMBER 07, 2013 学习资料 文章 Beginning Auto ...
- ios xib和代码的frame布局 iOSXib布局后代码修改约束的值
如何修改autolayout 约束的值? 1 2 3 4 5 6 目前我已知的方法有5种 1.修改frame(有时候可能会不起作用,但可以做动画) 2.修改约束的float值 3.使用VisualFo ...
- 【转】iOS6中的Auto Layout:通过代码添加约束
最近做的项目用到了Auto Layout,于是经过了一番大量的google,这是我看到的讲用代码创建约束最清晰的一篇教程,于是想跟更多的人分享一下.原文也比较简单,可以直接过去看,如果我翻译的 ...
- iOSXib布局后代码修改约束的值
如何修改autolayout 约束的值? 目前我已知的方法有5种 1.修改frame(有时候可能会不起作用,但可以做动画) 2.修改约束的float值 3.使用VisualFormat 语言 4. ...
- 【原】使用Bmob作为iOS后台开发心得——云端代码添加其他User的Relation关系
本文转载请注明出处 —— polobymulberry-博客园 问题描述 我在User表中增加了两个列,分别为“我关注的人”(Relation关系)和“我的粉丝”(Relation关系)当我关注某个人 ...
- h5单页面布局
前段时间做了一个PC端单页面应用 GitHub因为项目开始的比较仓促,加上本人前端经验特别少,虽然项目大体完成了,但是页面布局确成立它的硬伤...为了填补心里落差,专门做了一个h5的单页面布局,代码很 ...
- iOS代码添加视图约束
项目要做这样一个效果的启动页. 考虑到版本号是会不断变更的,因此采用动画效果启动页,让版本号动态加载iOS启动页动画效果 - 简书 考虑到屏幕适配问题,因此采用代码对视图添加约束.在添加约束的过程中遇 ...
- Web前端代码规范与页面布局
一. 规范目的: 为提高工作效率,便于后台人员添加功能及前端后期优化维护,输出高质量的文档,在网站建设中,使结构更加清晰,代码简明有序,有一个更好的前端架构,有利于SEO优化. 二. ...
- iOS自动布局高级用法 && 纯代码约束写法
本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...
随机推荐
- HDU4499
In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move horizontally or ...
- centos7.1部署java环境服务器
1.检查操作系统自带java是jdk还是jre(否有javac,本例中没有javac) [root@bogon ~]# ls -l /usr/lib/jvm/总用量 0drwxr-xr-x. 3 ro ...
- 3---Django rest framework源码分析(3)----节流
Django rest framework源码分析(3)----节流 目录 添加节流 自定义节流的方法 限制60s内只能访问3次 (1)API文件夹下面新建throttle.py,代码如下: # u ...
- Xlua文件在热更新中调用方法
Xlua文件在热更新中调用方法 public class news : MonoBehaviour { LuaEnv luaEnv;//定义Lua初始变量 void Awake() { luaEnv ...
- Oracle 角色及其权限
一.简介 Oracle权限分为系统权限和对象权限. 1.系统权限 注意:系统权限不支持级联回收,所以你需要使用sysdba一个个的回收. 2.对象权限 注:对象权限支持级联回收,系统权限不支持级联回收 ...
- jquery 添加关键字小插件
模块化封装,兼容seajs /** * User: c3t * Date: 14-3-25 * Time: 下午4:16 */ define(function (require, exports, m ...
- Best HTTP
http://blog.csdn.net/u012322710/article/details/52860747 Best HTTP (Pro) 这是一款很多公司都在用的网页插件,感觉确实不错,分P ...
- linux下快速安装python3.xx
安装python3之前的准备工作: 当前环境是centos操作系统[已经安装了gcc++],在安装前需要安装zlib-devel包: yum install zlib-devel yum instal ...
- Oracle SQL语句优化34条
非常好用的SQL语句优化34条 1)选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 dri ...
- svn自动更新网站代码
运行环境说明:svn的执行目录: /usr/bin/svnsvn版本库目录: /usr/local/nginx/html/svnrepos/coinhello (其中svnrepos是我各个项目版本库 ...