ios NSLayoutConstraint
为了让我们的应用在不同尺寸的屏幕下都能 “正常”的表示,我们尽量不要把数据写死。大多数可视元素都是一个矩形区域,当然这个矩形区域有坐标的,我们有了这个区域坐标就能确定可视元素的现实位置了。但是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的更多相关文章
- IOS NSLayoutConstraint 页面布局(通过代码添加约束)
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIVi ...
- ios Autolayout 按比例相对布局
看到一篇讲ios storyboard 按比例相对布局的博客,挺不错的转下来了! 可到liumh.com查看. 本文记录如何在 UIStoryboard 或者 xib 中进行百分比布局,包括 View ...
- Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 (主要是NSLayoutConstraint 的使用)
当前位置: > Swift新手入门 > Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 时间:2014-09-10 16:49来源:未知 作者:啊成 举报 点击:5 ...
- iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)
iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...
- 关于iOS 5 Could not instantiate class named NSLayoutConstraint错误
因为使用Xcode 4.6.2,新建工程的时候SDK 6.1,但是要做低版本适配.在将iOS模拟器选为5.0编译运行时候出现Could not instantiate class named NSLa ...
- iOS 5解决Could not instantiate class named NSLayoutConstraint问题
如果使用Xcode 4.5来新建项目,默认是支持AutoLayout的,但是AutoLayout是iOS 6的新特性,如果在iOS 5的simulator上运行程序,会出现Could not inst ...
- IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载
http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...
- iOS开发NSLayoutConstraint代码自动布局
1.NSLayoutConstraint简介 适配界面大多用Masonry工具,也是基于NSLayoutConstraint写的!通过使用两个类方法实现自动布局: + (NSArray<__ki ...
- 【iOS】屏幕适配之NSLayoutConstraint
前言 如何实现一张图片在iPhone和iPad上显示不同的尺寸,我了解到一般有三种办法:直接手写代码动态添加约束:把NSLayoutConstraint关联到ViewController里再viewD ...
随机推荐
- PHP简易聊天室&调试问题
在进入login.php程序之后 <?php error_reporting(E_ALL^E_NOTICE); session_start(); //装载Session库,一定要放在首行 $u ...
- ACM数论之旅7---欧拉函数的证明及代码实现(我会证明都是骗人的╮( ̄▽ ̄)╭)
欧拉函数,用φ(n)表示 欧拉函数是求小于等于n的数中与n互质的数的数目 辣么,怎么求哩?~(-o ̄▽ ̄)-o 可以先在1到n-1中找到与n不互质的数,然后把他们减掉 比如φ(12) 把12质因数分解 ...
- 45个实用的JavaScript技巧、窍门和最佳实践
在这篇文章中,我将分享一组JavaScript的技巧.窍门和最佳实践,这些都是JavaScript程序员应该知晓的,不管他们是使用在浏览器/引擎上,还是服务器端(SSJS——Service Side ...
- 利用Jquery的load函数实现页面的动态加载
利用Jquery的load函数实现页面的动态加载 js的强大功能相信大家都知晓,今天通过jquery的库函数load可以更加方便的实现页面的动态刷新,经过几天的研究与探索,终于有所成效!吾心甚蔚! ...
- 使用 Github Pages 发布你的项目文档
导读 你可能比较熟悉如何用 Github Pages 来分享你的工作,又或许你看过一堂教你建立你的第一个 Github Pages 网站的教程.近期 Github Pages 的改进使得从不同的数据源 ...
- leetcode 解题报告 Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [lintcode 14] First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- 我所理解的cocos2dx自适配屏幕大小方案
这里主要有两个点: 1.屏幕大小的设置,也就是手机窗口的大小,在各个手机上面或者平板上的屏幕的大小. 这个大小的设置就是代码里面的:glview->setFrameSize(width, hig ...
- 2016年11月10日--CSS动画
jquery动画:http://www.w3school.com.cn/jquery/jquery_animate.aspCSS3动画教程1:http://www.w3school.com.cn/cs ...
- Vijos P1769 网络的关键边
Description 一个连通的无向图,有些点有A属性,有些点有B属性,可以同时具有.删掉某条边后,如果使得连通块中一些点不具有A,B属性的点,求这些边. Sol Tarjan求割边. 首先这些边一 ...