constraintWithItem:函数
来源:互联网 发布:如何购买网络域名 编辑:程序博客网 时间:2024/06/11 10:00
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
参数说明:
参数1: 第一个视图view1
参数2: 为view1设置的一个参数,是一个NSLayoutAttribute的枚举:
NSLayoutAttributeLeft = 1, //视图的左边 NSLayoutAttributeRight, //视图的右边 NSLayoutAttributeTop, //视图的上边 NSLayoutAttributeBottom, //视图的下边 NSLayoutAttributeLeading, //视图的前面 NSLayoutAttributeTrailing, //视图的后面 NSLayoutAttributeWidth, //视图的宽度 NSLayoutAttributeHeight, //视图的高度 NSLayoutAttributeCenterX, //视图中心点的X坐标 NSLayoutAttributeCenterY, //视图中心点的Y坐标 NSLayoutAttributeBaseline, //视图的基准线 NSLayoutAttributeNotAnAttribute = 0 //无
参数3: 和另一个视图的属性之间的关系,是一个NSLayoutRelation的枚举:
NSLayoutRelationLessThanOrEqual = -1, //小于或等于 NSLayoutRelationEqual = 0, //等于 NSLayoutRelationGreaterThanOrEqual = 1, //大于或等于
参数4: 另一个视图view2
参数5: 为view2设置的一个参数,是一个NSLayoutAttribute的枚举,枚举内容同上
参数6: 与view2属性相乘的数multiplier
参数7: 与view2属性相加的数constant
这个方法可以理解为: 的 <某个属性> <大于/小于/等于> 的 <某个属性> 乘以 <一个数> 再加上 <一个数>
示例代码:
UIButton *button = [[UIButton alloc] init];//这里不再需要指定x.y等坐标.[button setTitle:@"Hello World" forState:UIControlStateNormal];[button setBackgroundColor:[UIColor redColor]];[button sizeToFit];//使用AutoLayout的方式来布局(为了不和autosizing冲突,我们设置NO)//意思就是遵循autoLayout抛弃原有设置的高度宽度等,使用autolayout的视图必须要设置该属性。[button setTranslatesAutoresizingMaskIntoConstraints:NO];[self.view addSubview:button];//创建水平居中父视图的约束//button中心点的X坐标等于self.view中心点的X坐标乘以1.0加上0NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:00.0f];//将约束1添加到对应的父视图中[self.view addConstraint:constraint1];//创建垂直居中父视图的约束//button中心点的Y坐标等于self.view中心点的Y坐标乘以1.0加上0NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0];//将约束2添加到对应的父视图中[self.view addConstraint:constraint2];//创建设置高度是父视图高度的1/3//button的高度等于self.view的高度乘以0.3加上0NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.3 constant:0.0];//将约束3添加到对应的父视图中[self.view addConstraint:constraint3];
补充:
如果想设置的约束里不需要view2,直接将第四个参数设为nil,第五个参数设为NSLayoutAttributeNotAnAttribute
NSLayoutAttributeLeft/NSLayoutAttributeRight 和 NSLayoutAttributeLeading/NSLayoutAttributeTrailing的区别是left/right永远是指左右,但是leading/trailing在某些从右至左习惯的地区会变成,leading是右边,trailing是左边。
1 0
- constraintWithItem:函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 函数
- 查询病案首页最后保存时间
- kalman算法原理
- GCD详解
- 浅谈KVC和KVO
- ${}没有数据
- constraintWithItem:函数
- NYOJ_116_士兵杀敌(二)插点问线
- 针对中科院java接口的使用方法和问题
- WordPress主题 Music Pro 高级音乐/电台自适应音乐销售模板[更新至v3.1]
- 【分组背包】HDU1712ACboy needs your help
- [LeetCode] 196. Delete Duplicate Emails
- Lua基础
- JNI - Access an instance Field
- php实现基于微信公众平台开发SDK(demo)扩展的方法