#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 页面布局(通过代码添加约束)的更多相关文章

  1. 【转】iOS学习之Autolayout(代码添加约束) -- 不错不错

    原文网址:http://www.cnblogs.com/HypeCheng/articles/4192154.html DECEMBER 07, 2013 学习资料 文章 Beginning Auto ...

  2. ios xib和代码的frame布局 iOSXib布局后代码修改约束的值

    如何修改autolayout 约束的值? 1 2 3 4 5 6 目前我已知的方法有5种 1.修改frame(有时候可能会不起作用,但可以做动画) 2.修改约束的float值 3.使用VisualFo ...

  3. 【转】iOS6中的Auto Layout:通过代码添加约束

        最近做的项目用到了Auto Layout,于是经过了一番大量的google,这是我看到的讲用代码创建约束最清晰的一篇教程,于是想跟更多的人分享一下.原文也比较简单,可以直接过去看,如果我翻译的 ...

  4. iOSXib布局后代码修改约束的值

      如何修改autolayout 约束的值? 目前我已知的方法有5种 1.修改frame(有时候可能会不起作用,但可以做动画) 2.修改约束的float值 3.使用VisualFormat 语言 4. ...

  5. 【原】使用Bmob作为iOS后台开发心得——云端代码添加其他User的Relation关系

    本文转载请注明出处 —— polobymulberry-博客园 问题描述 我在User表中增加了两个列,分别为“我关注的人”(Relation关系)和“我的粉丝”(Relation关系)当我关注某个人 ...

  6. h5单页面布局

    前段时间做了一个PC端单页面应用 GitHub因为项目开始的比较仓促,加上本人前端经验特别少,虽然项目大体完成了,但是页面布局确成立它的硬伤...为了填补心里落差,专门做了一个h5的单页面布局,代码很 ...

  7. iOS代码添加视图约束

    项目要做这样一个效果的启动页. 考虑到版本号是会不断变更的,因此采用动画效果启动页,让版本号动态加载iOS启动页动画效果 - 简书 考虑到屏幕适配问题,因此采用代码对视图添加约束.在添加约束的过程中遇 ...

  8. Web前端代码规范与页面布局

    一.    规范目的: 为提高工作效率,便于后台人员添加功能及前端后期优化维护,输出高质量的文档,在网站建设中,使结构更加清晰,代码简明有序,有一个更好的前端架构,有利于SEO优化.   二.     ...

  9. iOS自动布局高级用法 && 纯代码约束写法

    本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...

随机推荐

  1. [V1-Team] WEDO创意论坛功能规格说明书

    项目功能规格说明书 版本说明 版本 内容 时间 V1.0 描述总体目标,用户使用场景,界面原型.功能设计及验收 2019.3.28 附Github仓库:WEDO 正文 1.目标 规范指导整个项目设计与 ...

  2. docker 容器启动的模板以及注意事项

    docker启动容器常用模板 docker run -dit \ -p : -p : -p : \-v /usr/local/xd_data:/usr/local/xd_data \-v /etc/l ...

  3. GraphQL 暂停

    别人的文章 http://blog.csdn.net/imwebteam/article/details/53888708 Java 文档都打不开,已经在GitHub上提了 in README.md ...

  4. linux 基础运维 之 Linux的闹钟

    1. linux 删除一个文件的权限要看文件所在的目录的权限 删除文件需要对对这个目录拥有w权限 修改文件 查看文件的内容需要对文件有rw权限 删除 创建一个文件 需要对文件坐在地目录拥有wx权限2. ...

  5. HDFS基本shell操作

    在客户端输入Hadoop fs,可以查看所有的,hadoop shell # -help [cmd] //显示命令的帮助信息,如: hadoop fs -help ls # -ls(r) <pa ...

  6. Ubuntu安装Docker步骤

    环境:Ubuntu Trusty 14.04 (LTS) 前提条件: Docker requires a 64-bit installation regardless of your Ubuntu v ...

  7. Java中forEach, 用来遍历数组

    这里的for是Java中forEach, 用来遍历数组的.for(int i : d) 就是遍历int型数组d的 每一次访问数组d的时候读取的数据放入int型的i中.和for(int i=0;i< ...

  8. 通过JavaScript动态生成html控件

    示例代码 <html> <head> <meta http-equiv="Content-Type" content="text/html& ...

  9. SpringBoot | 第三十章:Spring-data-jpa的集成和使用

    前言 在前面的第九章:Mybatis-plus的集成和使用章节中,介绍了使用ORM框架mybatis-plus进行数据库的访问.今天,我们来简单学习下如何使用spring-data-jpa进行数据库的 ...

  10. JS自定义手机端H5键盘

    在输入车牌号的时候,因为很多车牌号都是数字字母混合排列的,所以如果用输入法输入就需要频繁切换数字跟字母,有点麻烦. 在这里我们就用自定义一个弹出框代替键盘来使用. 1.首先,要禁止掉文本框弹出输入法, ...