当前位置: 首页 > news >正文

(八十八)VFL语言初步 - 实现布局

【基本的语法】

VFL的语法为H:和V:开头。代表水平和垂直。

接下来假设要涉及距离。使用|-x-,x为距离的点数。

对于视图。用[ ]包围,比如[blueView]。

①以下的语句实现了blueView水平方向左右各距离控制器的边缘20点:

H:|-20-[blueView]-20|

②假设要指定宽高,在视图名称之后用圆括号内填入常量数值,以下的代码实现了blueView距离左边20点,宽度固定为120点:

H:|-20-[blueView(20)]

③假设要指定相等关系,比如redView的宽度和blueView相等,则在开头为H:的条件下写[redView(==blueView)]。

④假设出现乘除计算,不能使用VFL,这时候应该使用NSLayoutConstraint的constraintWithItem:::::方法,计算公式为firstItem.x = (secondItem.x + constant) * multiplier,x为attribute指定的计算量。注意这个约束应该被加入到他们公共的父节点上。

【实现方法】

使用constraintsWithVisualFormat::::方法:

/**
     Format: VFL语句
     options:对齐方式
     metrics:VFL用到的变量
     views:VFL用到的视图
*/
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;


①假设不须要对齐,options填入0。

②views为在VFL中相应实际View的根据,比如@{@"redView":self.redView},当VFL中出现redView时。系统会通过views字典查找到self.redView,从而实现改动self.redView的尺寸。

③metrics为VFL中使用的变量。对于多次出现的值。能够用一个变量取代,然后在metrics中指定变量的值。


【实例】

以下的代码实现了blueView距离控制器View边缘左、中、右各20点,redView在blueView下20点,右对齐。宽度为blueView的一半。

须要注意的是禁用AutoResizing。

- (void)viewDidLoad {
    
    [super viewDidLoad];

    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSArray *blueViewHori = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:0 metrics:nil views:@{@"blueView":blueView}];
    [self.view addConstraints:blueViewHori];
    NSArray *blueRedVerti = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView(50)]-20-[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView":blueView,@"redView":redView}];
    [self.view addConstraints:blueRedVerti];
    
    NSLayoutConstraint *redViewWidthConstraint = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
    [self.view addConstraint:redViewWidthConstraint];
    
    
}


相关文章:

  • js中包含中文注释引起的错误
  • CSS3选择器:nth-child与:nth-of-type区别
  • 数据集成在ODS项目的应用模式
  • hadoop无法启动DataNode问题
  • 解决ajax.net 1.0中文乱码问题
  • 如何动态添加菜单/菜单项、子菜单、右键菜单
  • java泛型中?和T区别
  • 介绍一款开源的正则表达式测试工具-Regex Tester
  • Asp.Net中CS中获取物理路径
  • npm 安装
  • Delphi 与 DirectX 之 DelphiX(50): TDIB.DoLightness();
  • 小型企业局域网免费上网行为管理方案
  • docker~Dockerfile方式建立镜像HelloWorld
  • jQuery for Asp.Net 一步一步从入门到精通(附 jQuery API 彩色大图)
  • 最新如何解决git 输入github时每次都要输入用户名和密码问题
  • 【comparator, comparable】小总结
  • overflow: hidden IE7无效
  • PHP 小技巧
  • React-生命周期杂记
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • vue--为什么data属性必须是一个函数
  • 阿里云购买磁盘后挂载
  • 成为一名优秀的Developer的书单
  • 目录与文件属性:编写ls
  • 设计模式 开闭原则
  • 使用 Xcode 的 Target 区分开发和生产环境
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ​卜东波研究员:高观点下的少儿计算思维
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (14)Hive调优——合并小文件
  • (八)Spring源码解析:Spring MVC
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (黑马C++)L06 重载与继承
  • (十六)Flask之蓝图
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (一一四)第九章编程练习
  • . Flume面试题
  • .htaccess配置重写url引擎
  • .net 4.0发布后不能正常显示图片问题
  • .net refrector
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .net使用excel的cells对象没有value方法——学习.net的Excel工作表问题
  • .NET性能优化(文摘)
  • [⑧ADRV902x]: Digital Pre-Distortion (DPD)学习笔记
  • [BZOJ 1032][JSOI2007]祖码Zuma(区间Dp)
  • [BZOJ] 2044: 三维导弹拦截
  • [BZOJ1053][HAOI2007]反素数ant
  • [hdu 4405] Aeroplane chess [概率DP 期望]
  • [NSSCTF]-Web:[SWPUCTF 2021 新生赛]easyrce解析
  • [rancher] rancher部署和使用的一些思考
  • [Ruby] 基础知识
  • [Spring Cloud 项目] Spring cloud 实现房源查询功能
  • [SQL]实现按照指定分割分分割字符串