Objectiv-C UIKit基础 NSLayoutConstraint的使用(VFL实现)
利用VFL可视化语言 (简单的抛砖引玉)
构建3个View 橙色和绿色左中右间隔20 上间隔40 高为200
蓝色在橙色内(0,0)处 宽高为橙色的一半
实现效果如下
由于atutosize和autolayout不兼容
首先构建3个view 将设atutosize为不可用
UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:orangeView];
UIView *greenView = [[UIView alloc] init];
greenView.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenView];
UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
[orangeView addSubview:blueView];
//设置atutosize属性为不可用
self.view.translatesAutoresizingMaskIntoConstraints = NO;
orangeView.translatesAutoresizingMaskIntoConstraints = NO;
greenView.translatesAutoresizingMaskIntoConstraints = NO;
blueView.translatesAutoresizingMaskIntoConstraints = NO;
- 设置约束
//设置orangeView,greenView水平方向约束
NSArray *conH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[view1]-20-[view2(==view1)]-20-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:@{@"view1":orangeView,@"view2":greenView}];
[self.view addConstraints:conH];
//设置orangeView,greenView垂直方向约束
NSArray *conV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[view1(200)]" options:0 metrics:nil views:@{@"view1":orangeView}];
[self.view addConstraints:conV];
//设置blueView水平方向约束
NSArray *conH2 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[view1]" options:0 metrics:nil views:@{@"view1":blueView}];
[orangeView addConstraints:conH2];
//设置blueView垂直方向约束
NSArray *conV2 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view1]" options:0 metrics:nil views:@{@"view1":blueView}];
[orangeView addConstraints:conV2];
//设置宽高约束
NSLayoutConstraint *conWidth = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:orangeView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
[orangeView addConstraint:conWidth];
NSLayoutConstraint *conHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:orangeView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0];
[orangeView addConstraint:conHeight];
下面来解释下VFL的使用
使用NSLayoutConstraint
类方法
+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(nullable NSDictionary<NSString> *)metrics views:(NSDictionary<NSString> *)views;
- 几个参数:
format
可视化语言
opts
NSLayoutFormatOptions
枚举 用来设置对齐
metrics
以字典的形式设置距离变量
比如 "H:|-[dis1]-[view1]-[dis2]-[view2(view1)]-20-|"这句中的[dis1] [dis2]为视图变量,将字典的view1 view2即为key 对应相应的视图
views
以字典的形式设置视图变量
比如 "H:|-20-[view1]-20-[view2(view1)]-20-|"这句中的[view1] [view2]为视图变量,将字典的view1 view2即为key 对应相应的视图
约束关系(与父类的关系)用到另一个类方法
+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
几个参数:
view1
view2
约束对象
attr1
attr2
属性包括 上下左右宽高中点等
relation
约束关系包括 相等 大于 小于
multiplier
需要修正的值
c
偏移量
在添加约束时 一定要记得是在父类上添加约束
比如 为 orangeView 和 greenView添加约束需要在其父类self.view上添加约束
blueView的父类是orangeView 所以给blueView添加约束时 在orangeView上添加
Objectiv-C UIKit基础 NSLayoutConstraint的使用(VFL实现)的更多相关文章
- iOS 开发实践之 Auto Layout
原:http://xuexuefeng.com/autolayout/?utm_source=tuicool 本文是博主 iOS 开发实践系列中的一篇,主要讲述 iOS 中 Auto Layout(自 ...
- [转载]前端——实用UI组件库
https://www.cnblogs.com/xuepei/p/7920888.html Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https:/ ...
- IOS的各种控件(转载,防止遗忘)
UITextView控件的详细讲解 感觉写的相当不错,而且很全就直接转载了 1.创建并初始化 创建UITextView的文件,并在.h文件中写入如下代码: #import <UIKit/UIKi ...
- 【转】前端——实用UI组件库
Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https://valor-software.com/ngx-bootstrap/#/ github: h ...
- 前端——实用UI组件库
Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https://valor-software.com/ngx-bootstrap/#/ github: h ...
- 基础框架Fundation和UIkit框架的定义和使用
Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...
- NSLayoutConstraint 使用详解 VFL使用介绍
注意 使用前必须先取消所有的你想设置View 的 Autoresizing 属性 因为 Autoresizing Layout不能共存 系统默认是 Autoresizing for v in su ...
- swift 基础小结02 -- VFL约束、属性的get和set方法、懒加载、方法替换
一.属性的get和set方法 1.自定义属性的set和get方法 private(set) var _image:UIImage? //自定义属性get,s ...
- IOS页面自动布局 之 NSLayoutConstraint基础篇
使用AutoLayout之前需要知道以下两点: 1.必须设置 translatesAutoresizingMaskIntoConstraints为NO. 2.如果是viewControl则AutoLa ...
随机推荐
- Nginx实用教程(一):启动、停止、重载配置
Nginx是一个功能强大的web服务器和负载均衡软件,由俄罗斯人开发.Nginx包括一个master进程和数个worker进程,master进程用于读取.解析配置文件和管理worker进程,worke ...
- Unslider Web前端框架之图片轮播
前端框架,前端组件,前端库,都是一个意思,能看源码. 最近做H5小游戏,用到了图片轮播的组件,而且要求支持移动端触屏滑动.一开始用的是nivo slider,但是对大小不一样的图不支持box 的参数设 ...
- jQuery时间日期插件laydate,兼容bootstrap
解压后,将laydate整个文件放至您项目的任意目录,不要移动其文件结构,它们具有完整的依赖体系. 使用只需在页面引入laydate.js即可. 如果您的网站的js采用合并或模块加载,您需要打开lay ...
- ionic复选框应用
如图:在项目中我需要实现这个效果布局和功能(进入页面默认全选,点击之后可以不选择) HTML代码: <div class="row" ng-repeat="engi ...
- VR虚拟现实技术在教育领域的前景展望
VR虚拟现实技术在教育领域的前景展望 VR虚拟现实技术能迅速火起来,是基于它突破了人们对三维空间在时间与地域上的感知限制,以及市场需求愿景的升级.此技术可广泛地应用到城市规划.室内设计.工业仿真.古迹 ...
- 数据处理包dplyr的函数
dplyr专注处理dataframe对象, 并提供更稳健的与其它数据库对象间的接口. 一.5个关键的数据处理函数: select() 返回列的子集filter() 返回行的子集arrang ...
- 详解 try-with-resource
[TOC] Oracle官方文档: http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources. ...
- chrome谷歌浏览器-DevTool开发者工具-详细总结
目录: 一.概述 1.官方文档 2.打开方法: 3.前言: 二.九个模块: 1.设备模式Device Mode 2.元素面板Elements 3.控制台面板Console 4.源代码面板Sources ...
- WCF学习——构建第二个WCF应用程序(四)
一.WCF服务端应用程序 1.创建WCF服务端应用程序项目 打开Visual Studio 2013,在菜单上点击文件->新建->项目->WCF服务应用程序.在弹出界面的" ...
- ajax数据请求5(php格式)
ajax数据请求5(php格式): <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...