Autolayout约束的代码实现方式-1
2016-04-11 21:50
711 查看
代码实现方式不比图形界面妥妥拽拽几下那么爽,一根线需要大概3行代码,所以导致一个完整约束的代码量看起来很繁琐。
以下为一个实例,表示一个蓝色的100*100的色块粘着着父控件右下角显示:
UIView *blueView = [[UIViewalloc]init];
blueView.backgroundColor = [UIColorblueColor];
[self.viewaddSubview:blueView];
blueView.translatesAutoresizingMaskIntoConstraints =NO;
// 宽度约束:100
NSLayoutCon
4000
straint *widthConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:100];
[blueView addConstraint:widthConstraint];
// 高度约束:100
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:100];
[blueView addConstraint:heightConstraint];
// 右边约束,距离父控件的右边有10的间距
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10];
[self.viewaddConstraint:rightConstraint];
// 底部约束,距离父控件的底部有10的间距
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-10];
[self.viewaddConstraint:bottomConstraint];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
这句代码表示如果使用了Autolayout自动布局方式的话,以前的Autoresizing模式下的相关的设定会自动转换成Autolayout模式下的约束,默认情况下是为YES,表示会自动转换,这就会导致下面的4个约束设定有可能会跟自动转换的约束发生冲突,所以每当使用Autolayout自动布局的时候,都应该把这个属性设置为NO。
参数解读:
* constraintWithItem:表示对哪个控件进行约束设置
* attribute:设置控件的哪个属性(宽,高,对上,下,左,右的间距)
* relatedBy:与下一个对比控件的关系,为等于(equal),大于(greater),小于(less)
* toItem:相对于哪一个控件(如果没有设置nil)
* attribute:相对的那个控件的哪个属性(如果没有设置NSLayoutAttributeNotAnAttribute)
* multiplier:乘数
* constant:加数
以下为一个实例,表示一个蓝色的100*100的色块粘着着父控件右下角显示:
UIView *blueView = [[UIViewalloc]init];
blueView.backgroundColor = [UIColorblueColor];
[self.viewaddSubview:blueView];
blueView.translatesAutoresizingMaskIntoConstraints =NO;
// 宽度约束:100
NSLayoutCon
4000
straint *widthConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:100];
[blueView addConstraint:widthConstraint];
// 高度约束:100
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:100];
[blueView addConstraint:heightConstraint];
// 右边约束,距离父控件的右边有10的间距
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:-10];
[self.viewaddConstraint:rightConstraint];
// 底部约束,距离父控件的底部有10的间距
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:blueView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-10];
[self.viewaddConstraint:bottomConstraint];
blueView.translatesAutoresizingMaskIntoConstraints = NO;
这句代码表示如果使用了Autolayout自动布局方式的话,以前的Autoresizing模式下的相关的设定会自动转换成Autolayout模式下的约束,默认情况下是为YES,表示会自动转换,这就会导致下面的4个约束设定有可能会跟自动转换的约束发生冲突,所以每当使用Autolayout自动布局的时候,都应该把这个属性设置为NO。
参数解读:
* constraintWithItem:表示对哪个控件进行约束设置
* attribute:设置控件的哪个属性(宽,高,对上,下,左,右的间距)
* relatedBy:与下一个对比控件的关系,为等于(equal),大于(greater),小于(less)
* toItem:相对于哪一个控件(如果没有设置nil)
* attribute:相对的那个控件的哪个属性(如果没有设置NSLayoutAttributeNotAnAttribute)
* multiplier:乘数
* constant:加数
相关文章推荐
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- 央视新闻报道XcodeGhost事件
- 原生JS仿苹果任务栏菜单,放大效果的菜单
- SQLserver删除某数据库中所有表实现思路
- SQLite 入门教程三 好多约束 Constraints
- MySQL学习笔记4:完整性约束限制字段
- mysql创建Bitmap_Join_Indexes中的约束与索引
- sql server建库、建表、建约束技巧
- SQL Server创建数据库和数据表的相关约束实现方法
- SQLSERVER实现更改表名,更改列名,更改约束代码
- Oracle 基础篇 --- 约束
- 约束类型介绍以及使用整理
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客