iOS 使用代码创建约束,实现自动布局
2024-08-25 03:32:17
///与下面约束对象属性截图相对应
//使用Auto Layout约束,禁止将Autoresizing Mask转换为约束
[self.funcView setTranslatesAutoresizingMaskIntoConstraints:NO];
/**
*@1.constraintWithItem:你想要约束的视图
*@2.attribute:约束的属性(枚举值,可使用com键查看)
*@3.relatedBy:与约束的父视图的关系(枚举值)
*@4.toItem:约束对象的父视图
*@5.attribute:约束的属性(枚举值,可使用com键查看),一般情况下与第二个参数写一样的
*@6.multiplier:相对于父视图的约束比例
*@7.constant:约束间隔
*/
//与约束视图与父视图宽度一致
NSLayoutConstraint *w = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
//与约束视图与父视图高度一致
NSLayoutConstraint *h = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
//与约束视图与父视图center.x一致
NSLayoutConstraint *x = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
//与约束视图与父视图center.y一致
NSLayoutConstraint *y = [NSLayoutConstraint constraintWithItem:_funcView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_fview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; //把约束添加到父视图上
NSArray *array = @[w,h,x,y];
[_fview addConstraints:array];
最新文章
- 字符编码笔记:ASCII,Unicode和UTF-8
- Django入门
- SQL语言基础
- SqlServer 挂载本地盘符到服务器端,方面备份还原
- [开源夏令营][四] Docker remote API 之 镜像篇
- Unbuntu 14.04 下chrome browser bookmark 显示中文乱码解决方案
- Java生成目录
- [SOJ]连通性问题
- c++ new delete 常踩的坑
- Android Stuido 提高开发效率的插件
- js监听浏览器离开页面操作
- shell for 循环
- 一个时代的终结:为什么是时候放弃ITOM四大巨头了?这对IT领导者来说意味着什么?
- Java+selenium 如何下拉移动滚动条【实战】
- Linux系统VIM编辑器管理(2)
- Android导入AS工程
- Shell中的case命令
- 三、synchronized同步锁
- Linux命令-权限管理命令:chmod
- PHP远程连接mysql