NSLayoutConstraint
来源:互联网 发布:js设置页面自动刷新 编辑:程序博客网 时间:2024/06/05 02:36
自己实践出来的,写个总结。
+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullableid)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
此方法各参数:第一个参数:指定约束的视图view1
第二个参数:指定view1的属性attr1
第三个参数:指定左右两边的视图的关系relation
第四个参数:指定约束右边的视图view2
第五个参数:指定view2的属性attr2
第六个参数:指定一个与view2属性相乘的乘数multiplier
第七个参数:指定一个与view2属性相加的浮点数c
属性说明:
目前为用过的有
NSLayoutAttributeLeft -->左边约束
NSLayoutAttributeRight-->右边约束
NSLayoutAttributeTop-->上边约束
NSLayoutAttributeBottom-->下边约束
NSLayoutAttributeLeading-->左边约束
NSLayoutAttributeTrailing-->又边约束
NSLayoutAttributeWidth-->宽度约束
NSLayoutAttributeHeight-->高度约束
NSLayoutAttributeCenterX-->水平约束
NSLayoutAttributeCenterY-->垂直约束
relation说明:
NSLayoutRelationLessThanOrEqual-->小于等于
NSLayoutRelationEqual -->等于
NSLayoutRelationGreaterThanOrEqual -->大于等于
依据的公式是:view1.attr1 = view2.attr2*m +c
以下是自定义几个ScrollView随window的大小变化而变化。
int customColumn = 4; int customRow =4; NSMutableArray *customviewArray = [[NSMutableArrayalloc] initWithCapacity:0]; for (int i =0; i< 4; i++) { for (int j=0; j < 4; j++) { NSScrollView *aview = [NSScrollViewnew]; [aview setTranslatesAutoresizingMaskIntoConstraints:NO]; [_myviewaddSubview:aview]; NSLayoutConstraint *bottom ; if (i ==0) { bottom = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeBottommultiplier:1.0fconstant:-1.0f]; }else{ // CustomSlotView *bottomview = [customviewArray objectAtIndex:i*customColumn+j-customColumn]; NSScrollView *bottomview = [customviewArrayobjectAtIndex:i*customColumn+j-customColumn]; bottom = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeBottomrelatedBy:NSLayoutRelationEqualtoItem:bottomview attribute:NSLayoutAttributeTopmultiplier:1.0fconstant:-10.0f]; // [bottomview removeConstraints:[bottomview constraints]]; } NSLayoutConstraint *top ; if (i == customRow -1) { top = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeTopmultiplier:1.0fconstant:0.0f]; } NSLayoutConstraint *left; if (j ==0) { left = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeLeadingmultiplier:1.0fconstant:1.0f]; }else{ // CustomSlotView *leftview = [customviewArray objectAtIndex:i*customColumn+j-1]; NSTextView *leftview = [customviewArrayobjectAtIndex:i*customColumn+j-1]; left = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:leftview attribute:NSLayoutAttributeTrailingmultiplier:1.0fconstant:10.0f]; // [leftview removeConstraints:[leftview constraints]]; } NSLayoutConstraint *right; if (j == customColumn -1) { right = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeTrailingrelatedBy:NSLayoutRelationEqualtoItem:_myviewattribute:NSLayoutAttributeTrailingmultiplier:1.0fconstant:0.0f]; } NSLayoutConstraint *width; NSLayoutConstraint *height; if (i*customColumn + j >0) { // CustomSlotView *nextView = [customviewArray objectAtIndex:i*customColumn+j-1]; NSTextView *nextView = [customviewArrayobjectAtIndex:i*customColumn+j-1]; width = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeWidthrelatedBy:NSLayoutRelationEqualtoItem:nextView attribute:NSLayoutAttributeWidthmultiplier:1.0fconstant:0.0f]; height = [NSLayoutConstraintconstraintWithItem:aviewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nextView attribute:NSLayoutAttributeHeightmultiplier:1.0fconstant:0.0f]; // [nextView removeConstraints:[nextView constraints]]; } bottom.active =YES; top.active =YES; left.active =YES; right.active =YES; height.active =YES; width.active =YES; [customviewArray addObject:aview]; } }
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- NSLayoutConstraint
- [iOS] NSLayoutConstraint
- HDU 2870 Largest Submatrix(最大子矩阵面积)
- 如何开启QtCreator的代码自动补全功能
- phpcmsv9实现手机端与PC端双模板的方法与详细教程(转)
- JSONObject的toBean 和 fromObject
- 如何判断一个数的二进制有多少个1
- NSLayoutConstraint
- Qt 5以上关于中文乱码解决
- D
- Eclipse快捷键
- Linux文件管理
- 洛谷 P3203 [HNOI2010]BOUNCE 弹飞绵羊(分块)
- 用MyEclipse10开发基于JAX-WS的Web Service实例,接口查询oracle数据库,SpringMVC controller调用接口,附带源码
- java.lang.OutOfMemoryError: Failed to allocate a 3110419 byte allocation with 741152 free bytes and
- 经典笔试题——去掉字符串中连续出现K个0的子串