为了让我们的应用在不同尺寸的屏幕下都能 “正常”的表示,我们尽量不要把数据写死。大多数可视元素都是一个矩形区域,当然这个矩形区域有坐标的,我们有了这个区域坐标就能确定可视元素的现实位置了。但是iphone5和以前的屏幕不一样了,在以前的设备中,我们可以添加一个 xx.@2x.png来适应retina屏幕,但是iphoen5咋办呢?ios6引入了 Auto Layout的东东,这个要和UIViewAutoresizing区分下。

1.看下面代码:

-(void)viewDidLoad

{

[superviewDidLoad];

UIView *aView =[[UIView alloc] init];

aView.backgroundColor = [UIColor redColor];

//为了不和autosizing冲突,我们设置No

[aView  setTranslatesAutoresizingMaskIntoConstraints:NO];//不把AutoresizingMask转化为Constraints

[self.view  addSubview:aView];

UIView *bView =[[UIView alloc] init];

bView.backgroundColor = [UIColor blueColor];

[bView  setTranslatesAutoresizingMaskIntoConstraints:NO];

[self.view  addSubview:bView];

NSDictionary*views =NSDictionaryOfVariableBindings(aView, bView);

/* This macro is a helper for making view dictionariesfor +constraintsWithVisualFormat:options:metrics:views:.

NSDictionaryOfVariableBindings(v1, v2, v3) isequivalent(等效于)  to [NSDictionarydictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3,@"v3", nil];

*/

[self.viewaddConstraints:

[NSLayoutConstraint   constraintsWithVisualFormat:@"H:|-(>=50)-[aView(100)]"

options:0

metrics:nil

views:views]];

[self.view  addConstraints:

[NSLayoutConstraint  constraintsWithVisualFormat:@"V:|-(>=100)-[aView(50)]"

options:0

metrics:nil

views:views]];

[self.viewaddConstraints:

[NSLayoutConstraint   constraintsWithVisualFormat:@"H:[bView(==aView)]"

options:0

metrics:nil

views:views]];

[self.view addConstraints:

[NSLayoutConstraint   constraintsWithVisualFormat:@"V:[bView(==aView)]"

options:0

metrics:nil

views:views]];

[self.viewaddConstraint:

[NSLayoutConstraint  constraintWithItem:bView

attribute:NSLayoutAttributeLeft

relatedBy:NSLayoutRelationEqual

toItem:aView

attribute:NSLayoutAttributeRight

multiplier:1

constant:10]];

//添加一个限制等效于bView.frame.origin.x = (aView.frame.origin.x +aView.frame.size.width) * 1  + 10!它是一种依赖关系,bView依赖aView,这样就算aView变了,bView也会跟着变换。

[self.viewaddConstraint:

[NSLayoutConstraint  constraintWithItem:bView

attribute:NSLayoutAttributeTop

relatedBy:NSLayoutRelationEqual

toItem:aView

attribute:NSLayoutAttributeTop

multiplier:1

constant:0]];

[aView release];

[bView release];

}

2.constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:

/* Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier
+ constant"

If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.

*/

3.属性

typedefNS_ENUM(NSInteger, NSLayoutAttribute) {

NSLayoutAttributeLeft = 1,

NSLayoutAttributeRight,

NSLayoutAttributeTop,

NSLayoutAttributeBottom,

NSLayoutAttributeLeading,

NSLayoutAttributeTrailing,

NSLayoutAttributeWidth,

NSLayoutAttributeHeight,

NSLayoutAttributeCenterX,

NSLayoutAttributeCenterY,

NSLayoutAttributeBaseline,

NSLayoutAttributeNotAnAttribute = 0

};

4.关系

typedefNS_ENUM(NSInteger, NSLayoutRelation) {

NSLayoutRelationLessThanOrEqual = -1,

NSLayoutRelationEqual = 0,

NSLayoutRelationGreaterThanOrEqual = 1,

};

最后的结果就是 “view1.attr1 <= 或者 ==   或者 >=  view2.attr2 * multiplier + constant”

5.Visual Format Language  (视觉形式语言)

ios NSLayoutConstraint的更多相关文章

  1. IOS NSLayoutConstraint 页面布局(通过代码添加约束)

    #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIVi ...

  2. ios Autolayout 按比例相对布局

    看到一篇讲ios storyboard 按比例相对布局的博客,挺不错的转下来了! 可到liumh.com查看. 本文记录如何在 UIStoryboard 或者 xib 中进行百分比布局,包括 View ...

  3. Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 (主要是NSLayoutConstraint 的使用)

    当前位置: > Swift新手入门 > Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 时间:2014-09-10 16:49来源:未知 作者:啊成 举报 点击:5 ...

  4. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  5. 关于iOS 5 Could not instantiate class named NSLayoutConstraint错误

    因为使用Xcode 4.6.2,新建工程的时候SDK 6.1,但是要做低版本适配.在将iOS模拟器选为5.0编译运行时候出现Could not instantiate class named NSLa ...

  6. iOS 5解决Could not instantiate class named NSLayoutConstraint问题

    如果使用Xcode 4.5来新建项目,默认是支持AutoLayout的,但是AutoLayout是iOS 6的新特性,如果在iOS 5的simulator上运行程序,会出现Could not inst ...

  7. IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载

    http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...

  8. iOS开发NSLayoutConstraint代码自动布局

    1.NSLayoutConstraint简介 适配界面大多用Masonry工具,也是基于NSLayoutConstraint写的!通过使用两个类方法实现自动布局: + (NSArray<__ki ...

  9. 【iOS】屏幕适配之NSLayoutConstraint

    前言 如何实现一张图片在iPhone和iPad上显示不同的尺寸,我了解到一般有三种办法:直接手写代码动态添加约束:把NSLayoutConstraint关联到ViewController里再viewD ...

随机推荐

  1. github及其他记录

    http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3 https://github.com/open-power-workgroup/Hospit ...

  2. VPN和SSH的原理区别

    原文:http://www.hostloc.com/thread-153223-1-1.html 看了http://www.hostloc.com/thread-153166-1-1.html 主要说 ...

  3. Mac OS X 11以上系统的Rootless机制问题

    由于项目紧,系统一直停留在10版本,最近清闲之后,第一件事就是升级了系统,到11El Capitan版本. 本来想着随便升级了,可能有好玩的东东,结果好玩的木有看见,项目开发环境崩溃了,何其衰耶? 废 ...

  4. unity 2d 和 NGUI layer

    http://blog.csdn.net/xtxy/article/details/37876825 在使用unity2d开发游戏的时候,使用了NGUI作为界面,本来二者配合得还挺好,但是一个使用场景 ...

  5. 跟着百度学PHP[4]OOP面对对象编程-5-内部引用$this

    $this就是对象内部代表这个对象的引用 可以调用被封装的方法或者属性! <?php class Person{ private $name; "; var $sex; functio ...

  6. eclipse中整合springMvc和velocity

    1.项目所需要的jar包(有些可能多余) 2.在src目录下创建一个bean  一个一个controller ,路径如下 person代码: package com.test.bean; import ...

  7. Android之Toast通知的几种自定义用法

    Toast.makeText(context, str,                 Toast.LENGTH_SHORT).show();     }                       ...

  8. linux下编译安装boost库

    linux下编译安装boost库 linux下编译安装boost库 1.下载并解压boost 1.58 源代码 下载 解压 2.运行bootstrap.sh 3.使用b2进行构建 构建成功的提示 4. ...

  9. Python进程、线程

    Lock & RLock :用来确保多线程多共享资源的访问. Semaphore : 用来确保一定资源多线程访问时的上限,例如资源池. Event : 是最简单的线程间通信的方式,一个线程可以 ...

  10. 11.5---含有空字符串的字符串查找(CC150)

    注意,1,"" 和 " ".是不同的,空字符串指的是"": 2,注意String的compareTo.小于是指<0.并不是==-1: ...