#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. ubuntu 安装ssh-server时出现错误:openssh-server: Depends: openssh-client (= 1:5.3p1-3ubuntu3) but 1:5.3p1-3ubuntu4 is to be installed

    错误如下: tiger@ubuntu:~/Desktop/work$ sudo apt-get install openssh-server [sudo] password for tiger: Re ...

  2. C#UDP同步实例

    差不多有一个礼拜总算有点进步. 代码很简单,只是为了实现功能. 网络上的资源是很多,除了不能用的,就是抄来抄去,是在乏味浪费时间. 说一下代码背景:实现的功能是发送端发送消息,接收端接收后立即响应,发 ...

  3. C++11无限制的unions

    [C++11无限制的unions] 在标准 C++ 中,并非任意的类型都能做为 union 的成员.比方说,带有 non-trivial 构造函数的类型就不能是 union 的成员.在新的标准里,移除 ...

  4. c++中指针类型在c#中怎么对应?

    int[] a=new int[5]; //取a[3]的地址 IntPtr addr=System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinned ...

  5. PostQueuedCompletionStatus详解

    PostQueuedCompletionStatus函数,向每个工作者线程都发送—个特殊的完成数据包.该函数会指示每个线程都“立即结束并退出”.下面是PostQueuedCompletionStatu ...

  6. Domino 迁移到Exchange 服务器 之在Domino Server 创建用户!

    我们打开Lotus Admin,导航到注册,点击到需要设置的人,然后再选择验证者标识符选择相应的组织配置标识符:

  7. 解决sencha touch显示.JSON包含中文数据时显示乱码问题

    按照ST官方示例navigationview做的一个示例.数据源是一个.json文件.但是显示的时候如果.json文件里有中文则乱码.我知道是编码问题,但是不知道怎么改,如何改. 问了N个人最后解决方 ...

  8. jQuery实现等比例缩放大图片让大图片自适应页面布局

    通常我们处理缩略图是使用后台代码(PHP..net.Java等)根据大图片生成一定尺寸的缩略图,来供前台页面调用,当然也有使用前台javascript脚本将加载后的大图强行缩放,变成所谓的缩略图,这种 ...

  9. AFNetWorking3.0使用 自签名证书的https请求

    前几日,项目组出于安全角度的考虑,要求项目中的请求使用https请求,因为是企业内部使用的app,因此使用了自签名的证书,而自签名的证书是不受信任的,所以我们就需要自己来做证书的验证,包括服务器验证客 ...

  10. 图片懒加载 lazyload

    添加引用 <script type="text/javascript" src="lazyload/yahoo-dom-event.js">< ...