1.Autolayout的2个核心概念

  • 1.参照
  • 2.约束

2.常见错误

  • 1.缺乏必要的约束, 比如
    只约束了宽度和高度, 没有约束具体的位置

  • 2.两个约束冲突, 比如
    1个约束控件的宽度为100, 1个约束控件的宽度为110

3.自动布局的核心计算公式

obj1.property1 =(obj2.property2 * multiplier)+ constant value

4.添加约束的规则

  • 在创建约束之后,需要将其添加到作用的view上
  • 在添加时要注意目标view需要遵循以下规则:

  • 1.对于两个同层级view之间的约束关系,添加到它们的父view上

  • 2.对于两个不同层级view之间的约束关系,添加到他们最近的共同父view上

  • 3.对于有层次关系的两个view之间的约束关系,添加到层次较高的父view上

5.核心方法

    /**  这个方法就是万能公式*  @param id 被约束控件对象*  @param attribute1: 被约束对象属性的那一边(上下左右)*  @param relatedBy 被约束对象与约束对象的关系(等于/大于等于/小于等于),比如等于NSLayoutRelationEqual*  @param toItem 参照对象*  @param attribute2 参照对象属性的那一边(上下左右)*  @param multiplier 乘数,相当于倍数;比如1,表示1倍;*  @param constant 常量;*  @return 约束好的对象的属性*/NSLayoutConstraint *blueViewLeft  =  [NSLayoutConstraint constraintWithItem:(id) attribute:(NSLayoutAttribute) relatedBy:(NSLayoutRelation) toItem:(id) attribute:(NSLayoutAttribute) multiplier:(CGFloat) constant:(CGFloat)];

6.开始代码示例

  • 1.创建蓝色的View
    UIView *blueView = [[UIView alloc] init];blueView.backgroundColor = [UIColor blueColor];// 禁止Autoresizing转为Autolayout的约束blueView.translatesAutoresizingMaskIntoConstraints = NO;[self.view addSubview:blueView];
  • 2.创建红色的View
    UIView *redView = [[UIView alloc] init];redView.backgroundColor = [UIColor redColor];// 禁止Autoresizing转为Autolayout的约束redView.translatesAutoresizingMaskIntoConstraints = NO;[self.view addSubview:redView];
  • 3./*** 添加蓝色控件的约束 ***/
    NSLayoutConstraint *blueLeftLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];[self.view addConstraint:blueLeftLc];NSLayoutConstraint *blueBottomLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];[self.view addConstraint:blueBottomLc];NSLayoutConstraint *blueRightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-20];[self.view addConstraint:blueRightLc];NSLayoutConstraint *blueHeightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:40];[blueView addConstraint:blueHeightLc];NSLayoutConstraint *blueHeightLc2 = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:100];// 设置约束对象高度2的优先级为50;blueHeightLc2.priority = 50;[blueView addConstraint:blueHeightLc2];NSLayoutConstraint *blueWidthLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];[self.view addConstraint:blueWidthLc];
  • 4./*** 添加红色控件的约束 ***/
    NSLayoutConstraint *redTopLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];[self.view addConstraint:redTopLc];NSLayoutConstraint *redBottomLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];[self.view addConstraint:redBottomLc];NSLayoutConstraint *redRightLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];[self.view addConstraint:redRightLc];

拥抱自动布局,忘掉frame吧!

转载于:https://www.cnblogs.com/lidaxia/p/4753464.html

Autolayout的简单介绍和示例代码相关推荐

  1. 【Unity3D插件】DoTween插件的简单介绍及示例代码

    推荐阅读 CSDN主页 GitHub开源地址 Unity3D插件分享 简书地址 我的个人博客 QQ群:1040082875 unity里面做插值动画的插件有许多,比较常见的有itween.hotwee ...

  2. Java制作圣诞树找规律_java 实现简单圣诞树的示例代码(圣诞节快乐)

    java 实现简单圣诞树的示例代码(圣诞节快乐) 代码如下: @Test public void shengdanshu(){ //叶子层 int level = 10; //根层 int rootL ...

  3. Python3.9的69个内置函数(内建函数)介绍,并附简单明了的示例代码

    Python 解释器内置(内建)了很多函数,这篇博文对它们进行逐一介绍,博主觉得有必要给出示例代码的会给出示例代码. 声明:博主(昊虹图像算法)写这篇博文时,用的Python的版本号为3.9.10. ...

  4. Redis简单命令(部分示例代码)

    一.redis文件夹下的可执行文件(文章尾部有示例代码) 可执行文件 作用 redis-server 启动redis redis-cli redis命令行工具 redis-benchmark 基准测试 ...

  5. python简单的聚类分析代码_python kmeans聚类简单介绍和实现代码

    一.k均值聚类的简单介绍 假设样本分为c类,每个类均存在一个中心点,通过随机生成c个中心点进行迭代,计算每个样本点到类中心的距离(可以自定义.常用的是欧式距离) 将该样本点归入到最短距离所在的类,重新 ...

  6. 常用数据结构与经典算法 简单讲解与示例代码

    数据结构与算法 数据结构与算法是一个学习计算机绕不过去的话题,而我们大学之中多数课程之中都使用伪代码进行讲解,给对我们的学习理解也是一把双刃剑,虽然可以让我们自己通过算法.思路自己写出程序,但也可能& ...

  7. android百度api配置,Android Studio 配置使用百度api (附带简单样例)(示例代码)

    还是和同学开发的那个课程作业项目的app, 要使用到百度地图的api 但是,官方文档貌似只有Eclipse的例子,对Android Studio似乎没有说明.  难道,是因为后者是 "Doo ...

  8. RandomAccessFile类的简单介绍,以及代码演示

    RandomAccessFile类,支持对文件的读取和写入.并且可以设置写入和读取的位置.该位置主要是以字节体现.相当于该文件存储在一个大型byte[] 数组.隐含存在一个指向该数组的指针.类似于该类 ...

  9. opencvpython图像代码_PythonOpenCV各种图像库的图像读写 增强 方式的简单介绍(附代码)...

    resize给定高和宽的像数值 img = cv2.imread('../96.jpg',0) # 0表示读入灰色图片,1表示读入彩色图片 resized = cv2.resize(img, (100 ...

最新文章

  1. 进击webpack4 (优化篇)
  2. 转贴: OpenGL开发库的组成
  3. JZOJ 4919. 【NOIP2017提高组模拟12.10】神炎皇
  4. 如何查找SAP Cloud for Customer UI页面对应的技术模型明细
  5. 在窗体上画图,并响应手标事件的实例
  6. xlrd合并单元格的读取的注意事项
  7. 断言java_Java几种常用的断言风格你怎么选?
  8. PyTorch 1.3发布!能在移动端部署,支持Colab云TPU,阿里云上也能用
  9. canal下载 linux_阿里canal数据库同步ES使用
  10. 调取方法_最新微信调取转账证据新方法公布
  11. ArcMAp10.2生成栅格数据时报错
  12. Dockerfile 数据卷最佳实践
  13. 验证码这种反人类的存在,浪费全球人口15万小时,有办法取缔吗?
  14. 计算机人工智能论文参考文献格式,人工智能论文参考文献范例借鉴
  15. 利用wordcloud和jieba制作词云
  16. Ai作图工业化流程工具
  17. Oracle中的日期时间函数
  18. 提示“The selection cannot be launched, and there are no recent launches.”
  19. pic c语言 位标志,PIC单片机状态寄存器中的C(进位/借位位标志)
  20. 柯基数据:先进的知识图谱技术,构建行业知识图谱,助企业打通内部信息孤岛,链接海量数据 |百万人学AI评选

热门文章

  1. 神经网络绘图工具-总结
  2. 用Java设计一个通讯录,保存读者的信息。
  3. mysql读写分离 存储过程_MySQL读写分离 · wang-y/Technical-Information Wiki · GitHub
  4. mysql 事务 不同库_MYSQL数据库重点:事务与锁机制
  5. java中notify是什么意思_java中wait,notify,notifyAll是什么?
  6. gerber文件怎么导贴片坐标_SMT贴片加工有哪些常用的名词
  7. 基于分割和识别的服饰商品的自动推荐
  8. 谷歌免费GPU,在线Jupyter notebook深度学习环境
  9. iis url重写 域名跳转子目录_IIS设置URL重写,实现页面的跳转的重定向方法
  10. java日期加一天_Java 关于日期加一天(日期往后多一天)