VFL语法-基础

  1 #import "ViewController.h"
  2
  3 @interface ViewController ()
  4 {
  5     UIView *_redView;
  6     UIView *_blueView;
  7     UIView *_cyanView;
  8 }
  9 @end
 10
 11 @implementation ViewController
 12
 13 - (void)viewDidLoad {
 14     [super viewDidLoad];
 15     //1.不再需要设置frame坐标
 16     _redView = [[UIView alloc] init];
 17     _redView.backgroundColor = [UIColor redColor];
 18     //2.可能会和 AutoresizingMask 冲突
 19     _redView.translatesAutoresizingMaskIntoConstraints = NO;
 20     [self.view addSubview:_redView];
 21
 22     _blueView = [[UIView alloc] init];
 23     _blueView.backgroundColor = [UIColor blueColor];
 24     _blueView.translatesAutoresizingMaskIntoConstraints = NO;
 25     [self.view addSubview:_blueView];
 26
 27     _cyanView = [[UIView alloc] init];
 28     _cyanView.backgroundColor = [UIColor cyanColor];
 29     _cyanView.translatesAutoresizingMaskIntoConstraints = NO;
 30     [self.view addSubview:_cyanView];
 31
 32     //约束
 33
 34     /*
 35     1.view1:给对象view1这个对象设置约束
 36     2.attribute: 指 view1.attribute 设置什么样的约束
 37     3.relatedBy: 关系 等于
 38     4.view2: 参照物
 39     5.attribute: view2.attribute
 40     6.multiplier:乘数因子
 41     7.constant:偏差值
 42
 43     8.计算: view1.attr1 = view2.attr3*multiplier + constant
 44     */
 45
 46     //创建距离父 view 的左间距约束
 47     NSLayoutConstraint *conLeft = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
 48     //创建右间距约束
 49     NSLayoutConstraint *conRight = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
 50     //创建top间距
 51     NSLayoutConstraint *conTop = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:20];
 52     //创建高度约束
 53     NSLayoutConstraint *conHeight = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.2 constant:0];
 54
 55     //创建下间距
 56     //NSLayoutConstraint *conBottom = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
 57
 58     [self.view addConstraints:@[conLeft,conRight,conTop,conHeight]];
 59
 60
 61
 62
 63     //NSLog(@"%@",NSStringFromCGRect(_redView.frame));
 64
 65     //创建距离父 view 的左间距约束
 66     NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
 67     //创建右间距约束
 68     NSLayoutConstraint *blueRight = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:0.5 constant:-10];
 69     //创建top间距
 70     NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_redView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
 71     //创建高度约束
 72     //NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.2 constant:0];
 73
 74     //创建下间距
 75     NSLayoutConstraint *blueBottom = [NSLayoutConstraint constraintWithItem:_blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
 76
 77     [self.view addConstraints:@[blueLeft,blueRight,blueTop,blueBottom]];
 78
 79
 80
 81
 82
 83     //创建距离父 view 的左间距约束
 84     //NSLayoutConstraint *cyanLeft = [NSLayoutConstraint constraintWithItem:_cyanView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:20];
 85     NSLayoutConstraint *cyanLeft = [NSLayoutConstraint constraintWithItem:_cyanView attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:0.5 constant:10];
 86     //创建右间距约束
 87     NSLayoutConstraint *cyanRight = [NSLayoutConstraint constraintWithItem:_cyanView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
 88     //创建top间距
 89     NSLayoutConstraint *cyanTop = [NSLayoutConstraint constraintWithItem:_cyanView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_redView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:20];
 90     //创建高度约束
 91     //NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:_redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.2 constant:0];
 92
 93     //创建下间距
 94     NSLayoutConstraint *cyanBottom = [NSLayoutConstraint constraintWithItem:_cyanView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
 95
 96     [self.view addConstraints:@[cyanLeft,cyanRight,cyanTop,cyanBottom]];
 97
 98 }
 99
100 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
101 {
102     NSLog(@"%@",NSStringFromCGRect(_redView.frame));
103 }
104
105 - (void)didReceiveMemoryWarning {
106     [super didReceiveMemoryWarning];
107
108 }
109
110 @end
时间: 2024-08-04 13:12:24

VFL语法-基础的相关文章

Java语法基础

Java语法基础 1.  关键字 某些单词对编译器有着特殊的含义,并且不能作为标示符使用,全部是小写字母 Java语言关键字 abstract boolean break byte case catch char class try do default continue double else extends assert final finally float for If implement import instanceof int interface long native new g

java语法基础一

Java语法基础一 Java代码基本格式 Java中所有程序代码都必须存在于一个类中,用class关键字定义类,在class之前可以有一些修饰符.格式如下: 修饰符 class 类名 { 程序代码 } 注:1.Java是严格区分大小写的. 2.Java程序中一句连续的字符串不能分开在两行中写. Java程序的注释 Java里的注释有三种类型: 1.单行注释 在注释内容前面加“//”,格式为: 代码; //注释内容 2.多行注释 以斜杠加星号开头,以星号加斜杠结尾. 3.文档注释 以斜杠加两个星号

Swift语法基础入门三(函数, 闭包)

Swift语法基础入门三(函数, 闭包) 函数: 函数是用来完成特定任务的独立的代码块.你给一个函数起一个合适的名字,用来标识函数做什么,并且当函数需要执行的时候,这个名字会被用于“调用”函数 格式: func 函数名称(参数名:参数类型, 参数名:参数类型...) -> 函数返回值 { 函数实现部分 } 没有参数没有返回值 可以写为 ->Void 可以写为 ->() 可以省略 Void.它其实是一个空的元组(tuple),没有任何元素,可以写成() func say() -> V

javascript语法基础-变量与函数

三 javascript语法基础-变量与函数 (一)变量的声明与运用 JavaScript中的变量与Java.C等强类型语言有很大区别,虽然在JavaScript中具有字符串.数字等数据类型. 变量申明语句的结构是var保留字加标识符,var和标识符之间用空格隔开. 赋值语句的结构是在变量和需要赋的值之间加上一个等号,例如a=1的含义是将变量a的值指定为1. 变量在定义的时候也可以同时赋值,如var a=1. PS:在变量使用前事先进行声明是个良好的编程习惯,这对将来学习Java等其他语言有帮助

php语法基础

php变量 php变量用于存储字符,数字,数组甚至对象资源等,以便在我们需要的地方使用. $变量名=值; 变量名以字母(a-z,A-Z)或者下划线_开始,后面可以跟 任意字母或数字以及下划线,但不能是空格. 例子: <?php $var_char="你好"; echo $var_char; ?> 结果为:你好! 延伸:与c语言等强类型的编程语言不通,php 是一门松散类型的语言,即不需要在设置变量之前 声明该变量.根据变量被设置的方式,php会自动 地将变量转换成正确的数据

C#-01.语法基础

a. 语法基础 i. 命名空间(namespace):是 C# 中组织代码的方式,用来声明命名空间 1. 语法:namespace 命名空间名称{ //命名空间的声明 } 2. 作用:可以把紧密相关的一些代码放在同一个命名空间中,大大提高管理和使用的效率 3. 与 Java 的不同处:Java 的是使用 package(包) 的关键字,作用是与 namespace 类似 i. using 关键字:用来引用其他命名空间 1. 语法:using 类名; 2. 与 Java 的不同处:Java 的是使

iOS复习笔记2:Objective-C语法基础

一 语法基础 1 关键字 关键字基本上都是以@开头,常见关键字如下: @interface,@implement,@end,@public,@private,@selector,@required,@encode等 其他id,self,super等 2 字符串以@开头 @"Hello world!" 3 布尔类型Yes/No 4 空类型nil(值为0) 5 其他C语言语法 二 OC的HelloWorld程序 // helloworld.m #import <Foundation/

java基础知识(2)---语法基础

二:java语法基础: 1,关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:其实就是还没有赋予特殊含义,但是准备日后要使用过的单词. 2,标示符:其实就是在程序中自定义的名词.比如类名,变量名,函数名.包含 0-9.a-z.$._ : 注意: 1),数字不可以开头. 2),不可以使用关键字. 3,常量:是在程序中的不会变化的数据. 4,变量:其实就是内存中的一个存储空间,用于存储常量数据. 作用:方便于运算.因为有些数据不确定.所以确定该数据的名词和存储空间. 特点:变量空间可以重复使用

c++笔记——语法基础1

c++笔记将记载一个温习路径,也算一个快速学习的参考吧!(不一定全面,想到什么就写什么.要学习的同志最好是有权威的书坐镇) 笔记大概会有如下有几个部分:语法基础,使用拓展库,实用工程,补充说明 数据类型: 简单类型:(byte = 8 bit,以下所示为32bit系统,bit不同稍有区别:除ab都可以在类型标识unsigned,表示无符号,一般最高位0表示正,1表示﹣,那么其取值范围也可以类推) a) void: 0byte 无值域 void b) bool: 1byte true/false