手写代码自动实现自动布局,即Auto Layout的使用,有需要的朋友可以参考下。

这里要注意几点:

    1. 对子视图的约束,若是基于父视图,要通过父视图去添加约束。

    1. 对子视图进行自动布局调整,首先对UIView的一个属性设置,这是因为如果我们用Interface Builder,勾选Ues Autolayout,这时autoresizingMask就会被Auto Layout 取代,在手写代码时,我们就需要手动控制,代码如下

      [_shadow setTranslatesAutoresizingMaskIntoConstraints:NO]

    1. 在添加约束前,要先将子视图添加到父视图上。

      以下为例,oneImgView 上添加子视图shadow,对shadow进行约束

@property (nonatomic, strong) UIImageView *shadow;
@property (nonatomic, strong) UIImageView *oneImgView;
[_shadow setTranslatesAutoresizingMaskIntoConstraints:NO]; // 使用自动布局
[self.oneImgView addSubview:_shadow]; // 添加约束前要先将子视图添加到父视图上
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:_shadow
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.oneImgView
attribute:NSLayoutAttributeWidth
multiplier:
constant:];
[self.oneImgView addConstraint:constraint]; constraint = [
NSLayoutConstraint
constraintWithItem:_shadow
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.oneImgView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f
];
[self.oneImgView addConstraint:constraint];
constraint = [
NSLayoutConstraint
constraintWithItem:_shadow
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeHeight
multiplier:1.0f
constant:58.0f
];
[self.oneImgView addConstraint:constraint];
constraint = [
NSLayoutConstraint
constraintWithItem:_shadow
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.oneImgView
attribute:NSLayoutAttributeLeft
multiplier:1.0f
constant:0.0f
];
[self.oneImgView addConstraint:constraint];

关于函数

    +(instancetype)constraintWithItem:(id)view1
attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation
toItem:(id)view2 attribute:(NSLayoutAttribute)attr2
multiplier:(CGFloat)multiplier     constant:(CGFloat)c;

苹果的官方文档是这么描述的。

/* 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.
*/

所以你只需要记住view1.attr1 = view2.attr2 * multiplier + constant就可以了

  可视化格式语言约束(VFL)

除此之外iOS 8推出了一种新的约束方式——可视化格式语言约束,这种约束方式和中国古代的象形文字很相似。它可以一次添加多个约束,而且很直观,但不是所有的约束都可以满足的,维护也有一定的难度

代码为例,如果我要实现shadow 距离父视图左边界为20,宽为40,距离顶部20,高度最小30,不能超过100

     
     NSArray *constraints = nil;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[_shadow(==40)]|" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(_shadow)];
[self.oneImgView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[_shadow(>=58)@100]" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(_shadow)];
[self.oneImgView addConstraints:constraints];

    1. format:这里是你的VFL语句,比如:@”H:|[_shadow]|”(注意“-”默认父子视图之间表示20px,同级视图之间表示8px,但是如果是“-10-”则表示前后相距10,短线距离忽略)
    1. options:默认写0,跟据需求去选择你想要的枚举
    1. metrics:
      字典,
      你的VFL语句中使用了动态参数时,将参数名和对应的值为键值对写在字典中,比如:@”H:|-[_shadow(==width)]-|”,表示这个
      _shadow的宽度为width,这是字典里面找到key对就的值,如果没有找到这个值,app就会crash.
    1. views:字典,你的VLF语句中使用到得所有view,用这个宏去添加NSDictionaryOfVariableBindings(view1,view2,view3…)

      上面代码中VFL语句为:@”H:|[_shadow]|”,其中“|”表示父视图的边界

以下为VLF语句

|: 表示父视图

-:表示距离

V: :表示垂直

H: :表示水平

>= :表示视图间距、宽度和高度必须大于或等于某个值

<= :表示视图间距、宽度和高度必须小宇或等于某个值

== :表示视图间距、宽度或者高度必须等于某个值

@ :>=、<=、== 限制 最大为 1000

以下为例

|-[view]-|: 视图处在父视图的左右边缘内

|-[view] : 视图处在父视图的左边缘

|[view] : 视图和父视图左边对齐

-[view]- : 设置视图的宽度高度

|-30.0-[view]-30.0-|: 表示离父视图 左右间距 30

[view(200.0)] : 表示视图宽度为 200.0

|-[view(view1)]-[view1]-| :表示视图宽度一样,并且在父视图左右边缘内

V:|-[view(50.0)] : 视图高度为 50

V:|-(==padding)-[imageView]->=0-[button]-(==padding)-| : 表示到父视图左边的距离为Padding,这两个视图间距必须大于或等于0并且距离底部父视图为 padding。

[wideView(>=60@700)] :视图的宽度为至少为60 不能超过 700

如果没有声明方向默认为 水平 V:

手写代码自动实现自动布局,即Auto Layout的使用的更多相关文章

  1. ClownFish:比手写代码还快的通用数据访问层

    http://www.cnblogs.com/fish-li/archive/2012/07/17/ClownFish.html 阅读目录 开始 ClownFish是什么? 比手写代码还快的执行速度 ...

  2. 手写代码UI,xib和StoryBoard间的的优劣比较

    在UI制作方面,逐渐分化三种主要流派:使用代码手写UI:使用单个xib文件组织viewController或者view:使用StoryBoard来通过单个或很少的几个文件构建UI.三种方式各有优劣,也 ...

  3. UI到底应该用xib/storyboard完成,还是用手写代码来完成?

    UI到底应该用xib/storyboard完成,还是用手写代码来完成? 文章来源:http://blog.csdn.net/libaineu2004/article/details/45488665 ...

  4. 2019前端面试系列——JS高频手写代码题

    实现 new 方法 /* * 1.创建一个空对象 * 2.链接到原型 * 3.绑定this值 * 4.返回新对象 */ // 第一种实现 function createNew() { let obj ...

  5. 前端面试手写代码——call、apply、bind

    1 call.apply.bind 用法及对比 1.1 Function.prototype 三者都是Function原型上的方法,所有函数都能调用它们 Function.prototype.call ...

  6. 如果选择构建ui界面方式,手写代码,xib和StoryBoard间的博弈

    代码手写UI这种方法经常被学院派的极客或者依赖多人合作的大型项目大规模使用. 大型多人合作项目使用代码构建UI,主要是看中纯代码在版本管理时的优势,检查追踪改动以及进行代码合并相对容易一些. 另外,代 ...

  7. .netER的未来路,关于基础是否重要和应该自己手写代码吗?

    http://www.cnblogs.com/onepiece_wang/p/5558341.html#!comments 引用"基础知识的学习,一开始可能是背书,但是在后续若干年的工作过程 ...

  8. Appium初始化设置:手写代码连接手机、appium-desktop连接手机

    一.包名获取的三种方式 1)找开发要2)mac使用命令:adb logcat | grep START win使用命令:adb logcat | findstr START 或者可以尝试使用第3条命令 ...

  9. gcd手写代码及STL中的使用方法

    一.手写代码 inline int gcd(int x,int y){ if(y==0) return x; else return(gcd(y,x%y)); } 二.STL中的使用方法 注:在STL ...

随机推荐

  1. IDEA之web项目(maven项目)创建

    1.下载IDEA付费版,有30天的试用期,免费版创建不了web项目(导入不了tomcat). 网址:IntelliJ IDEA :: Download Latest Version of Intell ...

  2. centos systemctl指令

    # systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...

  3. js计算24点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Python自动化之配置Python模块国内镜像

    Linux环境 在~/.pip/pip.conf文件中添加或修改 [global] index-url = http://mirrors.aliyun.com/pypi/simple/ [instal ...

  5. jquery 生成table表格 部分代码

    想生成上面这样的table表格先看返回数据格式 <div id="create_img_tab"></div> window.onload = functi ...

  6. Xcode开发技巧之Code Snippets Library

    http://blog.csdn.net/lin1986lin/article/details/21180007 目录(?)[-] 引言 什么是Code Snippets 如何新建Code Snipp ...

  7. 普元部署多个应用的方法(适用EOS6.5以上版本,且无需governor中添加应用)

    在EOS下跑default项目之外的另外一个项目,比如defaultNew 步骤1 安装EOS6.5,安装路径如下:E:\program\eos: 启动EOS Eos默认的应用名称为Default 步 ...

  8. oracle 存储过程中调用存储过程

    create procedure sp_name() begin ……… end 比如: create procedure pro_showdbs() show datebase; end //用ex ...

  9. android SDK manager 无法获取更新版本列表

    打开SDK Manager---Tools---Options,填入如下代理和端口,勾选选项也如下. 网址:mirrors.neusoft.edu.cn 端口:80 99%是成功的 参考:http:/ ...

  10. reportng的使用

    1.首先安装testng 2.下载reportng jar包 http://pan.baidu.com/s/1i3KdlQH 3.添加到project build path 注意:需要同时引入goog ...