利用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实现)的更多相关文章

  1. iOS 开发实践之 Auto Layout

    原:http://xuexuefeng.com/autolayout/?utm_source=tuicool 本文是博主 iOS 开发实践系列中的一篇,主要讲述 iOS 中 Auto Layout(自 ...

  2. [转载]前端——实用UI组件库

    https://www.cnblogs.com/xuepei/p/7920888.html Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https:/ ...

  3. IOS的各种控件(转载,防止遗忘)

    UITextView控件的详细讲解 感觉写的相当不错,而且很全就直接转载了 1.创建并初始化 创建UITextView的文件,并在.h文件中写入如下代码: #import <UIKit/UIKi ...

  4. 【转】前端——实用UI组件库

    Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https://valor-software.com/ngx-bootstrap/#/ github: h ...

  5. 前端——实用UI组件库

    Angular UI 组件 ngx-bootstrap 是一套Bootstrap 组件 官网:https://valor-software.com/ngx-bootstrap/#/ github: h ...

  6. 基础框架Fundation和UIkit框架的定义和使用

    Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...

  7. NSLayoutConstraint 使用详解 VFL使用介绍

    注意 使用前必须先取消所有的你想设置View 的 Autoresizing 属性 因为 Autoresizing  Layout不能共存  系统默认是 Autoresizing for v in su ...

  8. swift 基础小结02 -- VFL约束、属性的get和set方法、懒加载、方法替换

    一.属性的get和set方法          1.自定义属性的set和get方法          private(set) var _image:UIImage?     //自定义属性get,s ...

  9. IOS页面自动布局 之 NSLayoutConstraint基础篇

    使用AutoLayout之前需要知道以下两点: 1.必须设置 translatesAutoresizingMaskIntoConstraints为NO. 2.如果是viewControl则AutoLa ...

随机推荐

  1. (转)Javascript的DOM操作 - 性能优化

    转载:https://my.oschina.net/blogshi/blog/198910 摘要: 想稍微系统的说说对于DOM的操作,把Javascript和jQuery常用操作DOM的内容归纳成思维 ...

  2. oracle中的function的简单语法定义

    1. create or replace 函数名 (参数名 in 类型) return 返回值类型 as 定义变量 begin 函数体 end;

  3. logging模块

    要想使用好logging模块首先要知道它的使用流程: logging类的实例化:logger=logging.getLogger('') 设置logger的级别,logger.setLevel(log ...

  4. 解决Ubuntu SMPlayer播放视频无声音问题

    问题:Ubuntu Kylin 14.04 系统默认装好之后,smplayer播放视频都是正常的,但最近可能由于一些误设置,导致smplayer播放任何格式的视频都无声.解决方法:由于ALSA是Lin ...

  5. C#中==运算符

    在这篇博客中,我们将介绍如下内容: ==运算符与基元类型 ==运算符与引用类型 ==运算符与String类型 ==运算符与值类型 ==运算符与泛型 ==运算符与基元类型 我们分别用两种方式比较两个整数 ...

  6. 使用zabbix监控mysql的三种方式

    使用zabbix监控mysql的三种方式 1.只是安装agent 2.启用模板监控 3.启用自定义脚本的模板监控 zabbix中默认有mysql的监控模板.默认已经在zabbix2.2及以上的版本中. ...

  7. [leetcode-530-Minimum Absolute Difference in BST]

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  8. 【Android Developers Training】 108. 使用模拟定位进行测试

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  9. ReactJS基础(续)

    前边的ReactJS基础,我们可以了解到,对于React,可以说是万物皆组件 React的组件应该具有 可组合(Composeable)可重用(Reusable)可维护(Maintainable)的特 ...

  10. 统计学习方法 三 kNN

    KNN (一)KNN概念: K近邻算法是一种回归和分类算法,这主要讨论其分类概念: K近邻模型三要素: 1,距离: 2,K值的选择: K值选择过小:模型过复杂,近似误差减小,估计误差上升,出现过拟合 ...