博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发中利用VFL方法添加约束的基本操作步骤,便于自己查阅
阅读量:6851 次
发布时间:2019-06-26

本文共 1547 字,大约阅读时间需要 5 分钟。

hot3.png

UIButton *b1 = [UIButton buttonWithType:UIButtonTypeSystem];

    [b1 setTitle:@"确定" forState:UIControlStateNormal];

    b1.backgroundColor=[UIColor lightGrayColor];

    [self.view addSubview:b1];

    UIButton *b2 = [UIButton buttonWithType:UIButtonTypeSystem];

    [b2 setTitle:@"取消" forState:UIControlStateNormal];

    b2.backgroundColor=[UIColor lightGrayColor];

    [self.view addSubview:b2];

    UIButton *b3 = [UIButton buttonWithType:UIButtonTypeSystem];

    [b3 setTitle:@"重试" forState:UIControlStateNormal];

    b3.backgroundColor=[UIColor lightGrayColor];

    [self.view addSubview:b3];

    //预备步骤:关闭自动翻译约束功能

    b1.translatesAutoresizingMaskIntoConstraints = NO;

    b2.translatesAutoresizingMaskIntoConstraints = NO;

    b3.translatesAutoresizingMaskIntoConstraints = NO;

    // 此函数可以自动生成一个Dictionary

    // 将传入的对象引用作为value,将引用名变成字符串作为key

    // 传入的是 (b1,b2,b3)

    // 则生成的字典如下:

    // {@"b1":b1,@"b2":b2,@"b3":b3}

    NSDictionary *diction1 = NSDictionaryOfVariableBindings(b1,b2,b3);

    // vfl式子中的一些特殊含义的数字做一个名称对照表

    NSDictionary *diction2 = @{

@"top":@20,@"left":@20,@"right":@20,@"spacing":@10};

    //1.准备一个VFL

    NSString *hVFL = @"|-left-[b1]-spacing-[b2(b1)]-spacing-[b3(b1)]-right-|";

    //2.创建约束

    NSArray *cs1 = [NSLayoutConstraint constraintsWithVisualFormat:hVFL options:NSLayoutFormatAlignAllCenterY metrics:diction2 views:diction1];

    //3.将约束添加到父视图上

    [self.view addConstraints:cs1];

    //创建竖直方向的约束

    NSString *vVFL = @"V:|-top-[b1]";

    NSArray *cs2 = [NSLayoutConstraint constraintsWithVisualFormat:vVFL options:0 metrics:diction2 views:diction1];

    [self.view addConstraints:cs2];

转载于:https://my.oschina.net/iOSBoyJack/blog/509323

你可能感兴趣的文章
视频码率那些事
查看>>
Android仿网易云音乐:留声机效果
查看>>
vue-cli项目升级webpack4踩坑
查看>>
Python爬虫框架,内置微博、自如、豆瓣图书、拉勾、拼多多等规则
查看>>
android View 的绘制流程
查看>>
怎么实现mybatis半自动化解耦!看看资深程序员怎么说
查看>>
一个能拖动,能调整大小,能更新bind值的vue指令-vuedragx
查看>>
记一次基于vue-cli的多页面应用配置
查看>>
适用于小程序的 ES6
查看>>
Ribbon使用方法
查看>>
【译】将 Android 项目迁移到 Kotlin 语言
查看>>
vue 项目打包部署,通过nginx 解决跨域问题
查看>>
LightKV-高性能key-value存储组件
查看>>
小程序
查看>>
ES6变量的解构赋值
查看>>
ansible自动化运维详细教程及playbook详解
查看>>
快速解决Dev c++无法调试
查看>>
自学算法笔记
查看>>
python通过luhn算法实现的信用卡卡号验证源码
查看>>
小米手机5X获得Root权限的方法
查看>>