IOS6之AutoLayout(二)
来源:互联网 发布:站外优化的理解 编辑:程序博客网 时间:2024/06/08 16:34
IOS6之AutoLayout(一)
http://blog.csdn.net/zfpp25_/article/details/8861221
IOS6之AutoLayout(二)
http://blog.csdn.net/zfpp25_/article/details/8861855
IOS6之AutoLayout(三)
http://blog.csdn.net/zfpp25_/article/details/8861958
IOS6之AutoLayout(四)
http://blog.csdn.net/zfpp25_/article/details/8862040
IOS6之AutoLayout(五)
http://blog.csdn.net/zfpp25_/article/details/8862157
继IOS6之AutoLayout(一)简单讲解了的”上沿“、”下沿“、”左沿“、”右沿“相对布局方法之后,本篇讲解相对布局中的居中。
直接看实现代码:
- UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
- [secondButtonsetTitle:@"Second"forState:UIControlStateNormal];
- [secondButton sizeToFit];
- secondButton.translatesAutoresizingMaskIntoConstraints =NO;
- [self.viewaddSubview:secondButton];
- NSLayoutConstraint *constraint;
//secondButton的中心点的X坐标与self.view的中心点X坐标距离为零,也就是中心点X坐标重合
- constraint = [NSLayoutConstraint
- constraintWithItem:secondButton
- attribute:NSLayoutAttributeCenterX
- relatedBy:NSLayoutRelationEqual
- toItem:self.view
- attribute:NSLayoutAttributeCenterX
- multiplier:1.0f
- constant:0.0f];
- [self.view addConstraint:constraint];
//secondButton的中心点的X坐标与self.view的中心点Y坐标距离为零,也就是中心点Y坐标重合
- constraint = [NSLayoutConstraint
- constraintWithItem:secondButton
- attribute:NSLayoutAttributeCenterY
- relatedBy:NSLayoutRelationEqual
- toItem:self.view
- attribute:NSLayoutAttributeCenterY
- multiplier:1.0f
- constant:0.0f];
// 指定按钮的宽度为200.0f
- constraint = [NSLayoutConstraint
- constraintWithItem:secondButton
- attribute:NSLayoutAttributeWidth
- relatedBy:NSLayoutRelationEqual
- toItem: nil
- attribute:NSLayoutAttributeNotAnAttribute
- multiplier:1.0f
- constant:200.0f];
- [self.view addConstraint:constraint];
// 指定按钮的高度为60.0f
- constraint = [NSLayoutConstraint
- constraintWithItem:secondButton
- attribute:NSLayoutAttributeHeight
- relatedBy:NSLayoutRelationEqual
- toItem: nil
- attribute:NSLayoutAttributeNotAnAttribute
- multiplier:1.0f
- constant:60.0f];
- [self.view addConstraint:constraint];
以上代码中,指定按钮中心点的相对左边容易理解,但指定宽度和高度的时候,NSLayoutAttributeNotAnAttribute如何理解 ?
下面是官网的解释:
Constraints are of the form "view1.attr1 <relation> view2.attr2 * multiplier + constant". If the constraint you wish to express does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute
.
其实也不难理解了。如果有不理解内容可参考IOS6之AutoLayout(一)
转载请保留,原文链接:http://write.blog.csdn.net/postedit/8861855
若发现有不合适或错误之处,还请批评指正,不胜感激。
- IOS6之AutoLayout(二)
- IOS6之AutoLayout(二)
- IOS6之AutoLayout(二)
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(四)
- IOS6之AutoLayout(五)
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(四)
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(三)
- IOS6之AutoLayout(四)
- IOS6之AutoLayout(五)
- ios6 autoLayout
- AutoLayout(二)
- AutoLayout(二)
- autolayout 入门(二)
- 服务器框架搭建随笔
- 【Object-C】Get / Set 方法
- 算法导论 第23章 最小生成树 ,Kruskal算法,和Prim算法d
- Silverlight 常用小知识点总结
- IOS6之AutoLayout(一)
- IOS6之AutoLayout(二)
- 邮箱 POP3服务器(端口110) SMTP服务器(端口25)
- 数字证书
- IOS6之AutoLayout(三)
- 【Object-C】类Class
- JVM垃圾回收与内存分配(一)
- IOS6之AutoLayout(四)
- MFC消息映射与命令传递
- 如何查询Eclipse版本号