#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. Android教程说明-夜神模拟器连接IDE更新让Delphi发现你的手机或夜神模拟器

    相关资料: [深圳]jiuk 发布 1.官网下载模拟器http://www.bignox.com/并运行 2.打开开发者选项刚开始是看不到的->关于平板电脑->多点几次版本号->打开 ...

  2. labview在线帮助网址

    http://zone.ni.com/reference/zhs-XX/help/371361L-0118/ labview网络讲坛 网址 http://v.eepw.com.cn/video/com ...

  3. class str

    class str(object): """ str(object='') -> str str(bytes_or_buffer[, encoding[, erro ...

  4. css3 动画demo

    1)http://www.yyyweb.com/demo/css-cokecan/inner.html 2)页面切换效果demo http://www.yyyweb.com/demo/page-tra ...

  5. Oracle数据库编程:在JDBC中应用Oracle

    9.在JDBC中应用Oracle: JDBC访问数据库基本步骤:          1.加载驱动          2.获取链接对象          3.创建SQL语句          4.提交S ...

  6. (C#)使用队列(Queue)解决简单的并发问题

    (C#)使用队列(Queue)解决简单的并发问题 2015-07-16 13:04 13265人阅读 评论(8) 收藏 举报  分类: Asp.Net(8)  版权声明:本文为博主原创文章,未经博主允 ...

  7. DIV+CSS规范命名大全集合

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-06-19)   网页制作中规范使用DIV+CSS命名规则,可以改善优化功效特别是团队合作时候可以提供合作制作效率,具体DI ...

  8. map 转换 xml ; xml转map

    public class MessageKit { public static String map2xml(Map<String, String> map) throws IOExcep ...

  9. 用DataBaseMail发图片并茂的邮件

    不知道各位的老板有没有这样的要求, 一些系统中的数据需要定时发出邮件提醒, 如呆料就要到期或者一些待办的事项提醒. 当然这些用SSRS报表订阅可以实现,但有些公司没有设定相应的报表服务,又或者只是一些 ...

  10. 使用jquery获取父元素或父节点的方法

    今天面试题问到了,没答上,jq要继续学习啊 jquery获取父元素方法比较多,比如parent(),parents(),closest()这些都能帮你实现查找父元素或节点,下面我们来一一讲解: 先举个 ...