autolayout - sizeClass - 2
2015-06-23 21:29
351 查看
对于单个约束的文档详述。
/* 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. */ +(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c Description Creates a constraint that defines the relationship between the specified attributes of the given views. Constraints represent linear equations of the form view1.attr1 <relation> multiplier × view2.attr2 + c. If the constraint you wish to express does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute. Parameters view1 : The view for the left side of the constraint. attr1 : The attribute of the view for the left side of the constraint. relation : The relationship between the left side of the constraint and the right side of the constraint. view2 : The view for the right side of the constraint. attr2 : The attribute of the view for the right side of the constraint. multiplier : The constant multiplied with the attribute on the right side of the constraint as part of getting the modified attribute. c : The constant added to the multiplied attribute value on the right side of the constraint to yield the final modified attribute. Returns A constraint object relating the two provided views with the specified relation, attributes, multiplier, and constant.
对于NSLayoutAttribute的参数如下:每一个参数意义就不说了,自己查。
typedef NS_ENUM(NSInteger, NSLayoutAttribute) { NSLayoutAttributeLeft = 1, NSLayoutAttributeRight, NSLayoutAttributeTop, NSLayoutAttributeBottom, NSLayoutAttributeLeading, NSLayoutAttributeTrailing, NSLayoutAttributeWidth, NSLayoutAttributeHeight, NSLayoutAttributeCenterX, NSLayoutAttributeCenterY, NSLayoutAttributeBaseline, NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline, NSLayoutAttributeFirstBaseline NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeLeftMargin NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeRightMargin NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeTopMargin NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeBottomMargin NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeLeadingMargin NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeTrailingMargin NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeCenterXWithinMargins NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeCenterYWithinMargins NS_ENUM_***AILABLE_IOS(8_0), NSLayoutAttributeNotAnAttribute = 0 };
对于约束关系,relations只有大于等于,小于等于,等于这三个枚举值。
typedef NS_ENUM(NSInteger, NSLayoutRelation) { NSLayoutRelationLessThanOrEqual = -1, NSLayoutRelationEqual = 0, NSLayoutRelationGreaterThanOrEqual = 1, };
对于参数constant:c :
The constant added to the multiplied attribute value on the right side of the constraint to yield the final modified attribute.
实质上赋完参之后会发现其实就是一句VFL罢了,参照VFL的效果和格式就可以明白上面参数的意义了。
下面用前面的大牛的例子来详细解析一下:
//autolayout实现控件的平面中心水平和垂直居中:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"测试按钮" forState:UIControlStateNormal]; [button sizeToFit]; [button setBackgroundColor:[UIColor yellowColor]]; button.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:button]; NSLayoutConstraint *constraint; //水平居中 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]; [self.view addConstraint:constraint]; //垂直居中 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]; [self.view addConstraint:constraint];
相信例子不难大家都能懂,根据上面的文档:Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant"
代入公式可以得到:button.attributeCenterX LayoutRelationEqual self.view.attributeCenterX * multiplier + constant ;
其中:LayoutRelationEqul = '=' ,multiplier = 1.0f,constant = 0.0f 得到:
button.CenterX = self.view.CenterX
相当好理解吧!
同里在上篇文章中的例子:我们尝试代入:
//上边距 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:50.0f]; [self.view addConstraint:constraint]; //左边距 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:100.0f]; [self.view addConstraint:constraint]; //右边距 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-100.0f]; [self.view addConstraint:constraint]; //下边距 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:-350.0f]; [self.view addConstraint:constraint];
得到公式如下:
//上边距 : button.Top = self.view.Top + 50.0;
//下边距 : button.Bottom = self.view.Bottom - 350;
//左边距 : button.Left = self.view.Left + 100;
//右边距 : button.Right = self.view.Right - 100;
相当好理解吧!
再来一个例子,看看约束怎么限定一个控件的width和height。
/* 直接设定的控件尺寸 */ UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button setTitle:@"测试按钮" forState:UIControlStateNormal]; [button sizeToFit]; [button setBackgroundColor:[UIColor yellowColor]]; button.translatesAutoresizingMaskIntoConstraints = NO; [self.view addSubview:button]; NSLayoutConstraint *constraint; //设置宽度 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:130.0f]; [self.view addConstraint:constraint]; //设置高度 constraint = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:70.0f]; [self.view addConstraint:constraint]; NSLog(@"button autoChanged frame is :%@", NSStringFromCGRect(button.bounds));同样的道理按照上面的公式去套,得到公式如下:
//宽度 : button.width = 130;
//高度 : button.height = 70;
非常直观吧!
同时将button的bounds输出可以看到,还是sizeToFit的值哦。
待续...
如果以上引用大牛的例子造成任何不好的影响,请评论区通知小弟一声,马上修改。
相关文章推荐
- (六十八)使用XMPPFramework登录
- Android通讯录数据库介绍与基本操作(增删改查)
- MediaStore类使用Intent录制音频,拍照
- LaTeX 绘制表格
- (六十八)使用XMPPFramework登录
- html5
- WPF-提示窗(线程)
- oracle 数据恢复-闪回
- Python导入模块或是库文件的方法
- 工业互联网 + 的执行路线图
- poj1392 Ouroboros Snake 欧拉回路
- Mapped Statements collection does not contain value for后面是什么类什么方法之类的错误
- spring的事务处理
- python连续爬取多个网页的图片分别保存到不同的文件夹
- SEO实战经验分享 迎接百度算法新挑战
- 笔记本
- QuerySyntaxException
- Java基础--网络编程1
- android的布局xml文件如何添加注释?
- python连续爬取多个网页的图片分别保存到不同的文件夹