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以后 ...
随机推荐
- 采用MQTT协议实现android消息推送(4)选fusesource-mqtt-client为客户端
1.简介 一个java写的mqtt客户端.项目地址: https://github.com/fusesource/mqtt-client 2.引入fusesource-mqtt-client库 Fil ...
- 字符型图片验证码,使用tensorflow实现卷积神经网络,进行验证码识别CNN
本项目使用卷积神经网络识别字符型图片验证码,其基于 TensorFlow 框架.它封装了非常通用的校验.训练.验证.识别和调用 API,极大地减低了识别字符型验证码花费的时间和精力. 项目地址: ht ...
- IOS下去掉input submit圆角和
在iOS系统下input submit会有圆角,如果添加有背景色,背景色出错,在安卓系统是没有这些问题,可以在input样式加上这段样式 input{ -webkit-appearance: none ...
- 「bzoj1925」「Sdoi2010」地精部落 (计数型dp)
「bzoj1925」「Sdoi2010」地精部落---------------------------------------------------------------------------- ...
- 转帖 JS的基础语法
1.变量 1)变量是用来存储信息的容器. 2)在javascript中使用var 运算符(variable 的缩写)加变量名定义的. varx = 10; vary = 10.1; varz = “H ...
- oracle 备份恢复篇(五)---rman 剩下控制文件和spfile
一,环境准备 ❤ 拥有全量备份文件
- oem的使用
1 浏览器输入下面的网址: 虚拟机[安装orcale的机器]:http://localhost:1158/em/ 本机:http://192.168.47.10:1158/em/ 192.168.47 ...
- MYSQL系列-MYSQL基础增强(Myql函数)
MYSQL基础增强(Myql函数) 在这里只介绍一些常用的,比较新颖的: 字符串函数: CONCAT://字符串连接函数 mysql> SELECT CONCAT('My', 'S', 'QL' ...
- flask-restful 请求解析
基本参数 from flask import Flask from flask.ext.restful import reqparse, abort, Api, Resource app = Flas ...
- vim创建新的命令
转自:http://man.chinaunix.net/newsoft/vi/doc/usr_5F40.html#usr_40.txt *40.1* 键映射 简单的映射已经在 |05.3| 介绍过了. ...