#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad]; [self createViewWithConstraintItem];
[self createViewWithConstraint];
} - (void)createViewWithConstraintItem
{
UIView *redView = [[UIView alloc]init];
redView.backgroundColor=[UIColor redColor];
redView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:redView]; /**左边距50*/
NSLayoutConstraint *viewLeftConstraint = [NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view
attribute:NSLayoutAttributeLeading multiplier: constant:.f];
/**上边距50*/
NSLayoutConstraint *viewTopConstraint = [NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view
attribute:NSLayoutAttributeTop multiplier: constant:.f]; /*--------------------------------------------------------------------------------
注意:通常在设置宽度高度的时候最好不要固定死,另外当viw1.attribute不等于view2.attribute*multiplier
+constant的时候,我们则要constraintWithItem函数中的toItem设置为nil以及attribute参数设置
为NSLayoutAttributeNotAnAttribute
---------------------------------------------------------------------------------*/
/**宽度150*/
NSLayoutConstraint *viewWidthConstaint = [NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute multiplier: constant:];
/**高度150*/
NSLayoutConstraint *viewHeightConstaint = [NSLayoutConstraint constraintWithItem:redView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute multiplier: constant:]; [self.view addConstraints:@[viewLeftConstraint,viewTopConstraint,viewWidthConstaint,viewHeightConstaint]];
} - (void)createViewWithConstraint
{
/**创建左边view*/
UIView *leftView = [[UIView alloc]init];
leftView.backgroundColor = [UIColor redColor];
leftView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:leftView]; /**创建右边view*/
UIView *rightView = [[UIView alloc]init];
rightView.backgroundColor = [UIColor blueColor];
rightView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:rightView]; /**创建右下方view*/
UIView *rightBottomView = [[UIView alloc]init];
rightBottomView.backgroundColor = [UIColor yellowColor];
rightBottomView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:rightBottomView]; NSDictionary *viewDic = NSDictionaryOfVariableBindings(leftView,rightView,rightBottomView); /*距父视图左边距50以及自身宽度大于等于150*/
NSArray *leftView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[leftView(>=150)]" options: metrics:nil views:viewDic]; /*距父视图上边距100以及自身高度大于等于150*/
NSArray *leftView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[leftView(>=150)]" options: metrics:nil views:viewDic]; /*水平方向布局,rightView 在 leftView 右侧标准距离处,并且宽度不小于 50 点。*/
NSArray *rightView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[leftView]-[rightView(>=50)]" options: metrics:nil views:viewDic]; NSArray *rightView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[rightView(>=50)]" options: metrics:nil views:viewDic]; NSArray *rightBottomView_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[leftView]-[rightBottomView(>=50)]" options: metrics:nil views:viewDic]; /*垂直方向布局距离父视图100,另外rightBottomView 在 rightView下方也就是相当于紧贴*/
NSArray *rightBottomView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[rightView(>=50)][rightBottomView(>=100)]" options: metrics:nil views:viewDic]; [self.view addConstraints:leftView_H];
[self.view addConstraints:leftView_V]; [self.view addConstraints:rightView_H];
[self.view addConstraints:rightView_V]; [self.view addConstraints:rightBottomView_H];
[self.view addConstraints:rightBottomView_V]; } @end

使用autolayout的NSLayoutConstraint类中的constraintWithItem 、constraintsWithVisualFormat这两个类方法来创建视图并可以实现自动布局的更多相关文章

  1. Android编程心得-在任意类中获取当前屏幕宽高

    进行Android编程时,很多时候都需要获取当前屏幕的宽度与高度,但是当我们需要在别的类中调用屏幕宽高时,直接用原来的方法是不行的,下面我来介绍如何在任意类中调用宽度高度的两种方法. public v ...

  2. 类中为什么要定义__init__()方法

    总结一下, 加上__init__()方法后,类才可以实例化,不加类就是个空壳,相当于一个方法集合 学习Python的类,一直不太理解为什么一定要定义init()方法,现在简要谈一下自己的理解吧. 1. ...

  3. 多线程(Thread类中的方法线程名称)

    1 package multithread; 2 3 /* 4 * 如何创建一个线程呢? 5 * 6 * 创建线程方式一:继承Thread类. 7 * 8 * 步骤: 9 * 1,定义一个类继承Thr ...

  4. Java.lang.Integer类中toString(int i, int radix)的具体实现

    Java.lang.Integer.toString(int i,int radix)方法可以实现将一个int类型的10进制的数据转换为指定进制的数据. api文档中介绍: 返回第二个参数指定的基数中 ...

  5. 揭开C++类中虚表的“神秘面纱”

    C++类中的虚表结构是C++对象模型中一个重要的知识点,这里咱们就来深入分析下虚表的在内存中的结构. C++一个类中有虚函数的话就会有一个虚表指针,其指向对应的虚表,一般一个类只会有一个虚表,每个虚表 ...

  6. Python类中super()和__init__()的关系

    Python类中super()和__init__()的关系 1.单继承时super()和__init__()实现的功能是类似的 class Base(object): def __init__(sel ...

  7. Hibernate jpa 在实体类中对于时间的注解

    在时间类型DATE 属性上添加一个 @Temporal(TemporalType.DATE)(精确到年月日)@Temporal(TemporalType.TIME)(精确到时分秒)@Temporal( ...

  8. Java中是否可以调用一个类中的main方法?

    前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...

  9. Myeclipse中导入项目后java类中汉字注释出现乱码问题(已解决)

    今天重装系统,安装了新的Myeclipse后,导入之前的项目后,,出现了乱码问题.乱码问题主要是java类中的注释,而jsp页面中汉字却完好如初: 右键项目,查看项目的编码格式,UTF-8,把java ...

随机推荐

  1. Wicket Hello World Example

    A simple hello world example in Wicket, show the basic structure of Wicket web application. Tools an ...

  2. hdu 2199 Can you solve this equation?(高精度二分)

    http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS ...

  3. poj 3264 Balanced Lineup(RMQ裸题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 43168   Accepted: 20276 ...

  4. 技术文集:万能WINDOWS XP封装

    这里将系统封装分为3步:做系统.封装.部署 一.做系统 平台不限,但不建议在虚拟机上制作.CPU及主板芯片没有限制,关于intelide和intelppm的不兼容问题,深度白金3in1并没有删除这些注 ...

  5. Classifier4J的中文支持

    Classifier4J是一个轻量级的分类工具,支持贝叶斯分类.向量空间模型.信息摘要等.然而它却不支持中文,异常信息大致如下: Exception in thread "main" ...

  6. Openfire开发配置,Openfire源代码配置,OpenFire二次开发配置(eclipse)

    首先去官网把openfire的源码下下来: http://www.igniterealtime.org/downloads/source.jsp 1.下载后放到你的workspace当中,我的woek ...

  7. ios transition translate 闪屏问题总结

    webkit在绘制页面时会将结构分为各种层,当层足够大时就会变成很大的平铺层.这样一来webkit在每次页面结构发生变化时不需要都渲染整个页面而是渲染对应层了,这对渲染速度来说相当的重要.webkit ...

  8. Visual Studio无法添加断点

    今天在写代码的时候突然发现无法添加断点,更加详细的场景是“按F9可以添加调试行,但是断点不显示,且显示代码行数左边的灰色区域不见了”找了各种方法也没有解决,然后重启.修复甚至重装都不行,最后在万千网页 ...

  9. win8下hosts保存文档失败,提示:请检查文件是否被另一个应用程序打开

    选择文件,然后右键点击属性,然后进入"安全"选项卡下点击当前用户对用的用户名然后编辑权限,给予完全控制的权限. 如图: 图一:

  10. JavaWeb-10(会话技术之session&JSP)

    JavaWeb-会话技术之session&JSP 会话管理之Session技术 一.Session 在WEB开发中,server能够为每一个用户浏览器创建一个会话对象(session对象),注 ...