#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. EntityFramework简单例子

    @(编程) 这个例子是用vs2013连接mysql数据库. 1. NuGet安装EF和mysql 略 2. 对象 namespace EFDemo { class Student { public s ...

  2. 用 Python 脚本实现对 Linux 服务器的监控

    目前 Linux 下有一些使用 Python 语言编写的 Linux 系统监控工具 比如 inotify-sync(文件系统安全监控软件).glances(资源监控工具)在实际工作中,Linux 系统 ...

  3. JQuery中attr ,html,text,val,的一些用法

    attr:主要获取元素内部的属性,返回 的是属性值 html:返回当前元素(不包括他自己本身的标签,但是可以返回他自己的)的标签加上内容.仅限于返回第一个. text:和 .html() 方法不同, ...

  4. HDU3466Proud Merchants(贪心&背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目大意是说n个物品每个物品的花费是p,但是如果你现在的钱少于q就买不了这个物品,每个物品的价值是v,求有 ...

  5. Lotus 迁移到Exchange 2010 之准备使用Transport 同步Lotus 相关信息!

    这里我们先来分析下Lotus迁移到Exchange2010 的一些原理,由于存在一定周期的共存时间,因此在共存期间必须来实现相应的同步计划,整个同步计划包含了如下的同步计划:

  6. css控制图片自适应大小

    相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决.      该CSS的功能是:大于600的图片自动调整为600显示. <style ...

  7. TCP客户机-服务器

    1 僵尸进程 2 信号处理 信号: 1 由一进程发往另一进程 2 由内核发往某进程   僵尸状态: 父进程取回子进程的相关信息,进程的ID,终止状态,子进程的资源利用信息   编程时: 1 当派生子进 ...

  8. baseDao 使用spring3+hibernate4方式

    启动异常: java.lang.ClassCastException: org.springframework.orm.hibernate4.SessionHolder cannot be cast  ...

  9. jQuery打印Html页面自动分页

    最近项目中需要用到打印HTML页面,需要指定区域打印,使用jquery.PrintArea.js 插件 用法: $("div#printmain").printArea(); 但还 ...

  10. BZOJ 1497: [NOI2006]最大获利 最小割

    1497: [NOI2006]最大获利 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1497 Description 新的技术正冲击着手 ...