zoukankan      html  css  js  c++  java
  • Autolayout 总结

    使用代码实现Autolayout的方法1

    • 创建约束
    +(id)constraintWithItem:(id)view1
    attribute:(NSLayoutAttribute)attr1
    relatedBy:(NSLayoutRelation)relation
    toItem:(id)view2
    attribute:(NSLayoutAttribute)attr2
    multiplier:(CGFloat)multiplier
    constant:(CGFloat)c;
    * view1 :要约束的控件
    * attr1 :约束的类型(做怎样的约束)
    * relation :与参照控件之间的关系
    * view2 :参照的控件
    * attr2 :约束的类型(做怎样的约束)
    * multiplier :乘数
    * c :常量
    
    • 添加约束
    - (void)addConstraint:(NSLayoutConstraint *)constraint;
    - (void)addConstraints:(NSArray *)constraints;
    
    • 注意
      • 一定要在拥有父控件之后再添加约束
      • 关闭Autoresizing功能
      view.translatesAutoresizingMaskIntoConstraints = NO;
      

    使用代码实现Autolayout的方法2 - VFL

    • 使用VFL创建约束数组
    + (NSArray *)constraintsWithVisualFormat:(NSString *)format
    options:(NSLayoutFormatOptions)opts
    metrics:(NSDictionary *)metrics
    views:(NSDictionary *)views;
    * format :VFL语句
    * opts :约束类型
    * metrics :VFL语句中用到的具体数值
    * views :VFL语句中用到的控件
    
    • 使用下面的宏来自动生成views和metrics参数
    NSDictionaryOfVariableBindings(...)
    

    使用代码实现Autolayout的方法3 - Masonry

    • 使用步骤
      • 添加Masonry文件夹的所有源代码到项目中
      • 添加2个宏、导入主头文件
      // 只要添加了这个宏,就不用带mas_前缀
      #define MAS_SHORTHAND
      

    // 只要添加了这个宏,equalTo就等价于mas_equalTo

    define MAS_SHORTHAND_GLOBALS

    // 这个头文件一定要放在上面两个宏的后面

    import "Masonry.h"

    ```
    
    • 添加约束的方法
    // 这个方法只会添加新的约束
     [view makeConstraints:^(MASConstraintMaker *make) {
    
     }];
    
    // 这个方法会将以前的所有约束删掉,添加新的约束
     [view remakeConstraints:^(MASConstraintMaker *make) {
    
     }];
    
     // 这个方法将会覆盖以前的某些特定的约束
     [view updateConstraints:^(MASConstraintMaker *make) {
    
     }];
    
    • 约束的类型
    1.尺寸:widthheightsize
    2.边界:leftleading
    ight	railing	opottom
    3.中心点:centercenterXcenterY
    4.边界:edges
    
  • 相关阅读:
    python 基于os模块的常用操作
    python 文件的读写
    Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践
    Docker(四):Docker 三剑客之 Docker Compose
    Docker(三):Dockerfile 命令详解
    Docker(二):Dockerfile 使用介绍
    Docker(一):Docker入门教程
    虚拟机vmware centos7 扩展磁盘空间
    那些年我们遇到的坑(1)-Description Resource Path Location Type Archive for required library
    RPM安装命令总结
  • 原文地址:https://www.cnblogs.com/jasonduan/p/5217241.html
Copyright © 2011-2022 走看看