VFL语言的使用
1 //
2 // ViewController.m
3 // D01-VFL語言使用(了解)
4 //
5 // Created by apple on 15/8/8.
6 // Copyright (c) 2015年 apple. All rights reserved.
7 //
8
9
10 /**
11 1.添加一個blueView和redView在控制器的View中
12 2.約束blueView的左邊,右邊,上邊,距離控制器View的距離都為20;
13 3.約束blueView的高度為固定50;
14 4.約束redView和blueView右對齊;
15 5.約束redView的頂部距離blueView的底部為20的間距;
16 6.約束redView的寬度為blueView寬度的一半;
17 */
18 #import "ViewController.h"
19
20 @interface ViewController ()
21
22 @end
23
24 @implementation ViewController
25
26 - (void)viewDidLoad {
27 [super viewDidLoad];
28
29 UIView *blueView = [[UIView alloc] init];
30 blueView.backgroundColor = [UIColor blueColor];
31 [self.view addSubview:blueView];
32
33
34 UIView *redView = [[UIView alloc] init];
35 redView.backgroundColor = [UIColor redColor];
36 [self.view addSubview:redView];
37
38 // 禁用Autoresizing功能
39 blueView.translatesAutoresizingMaskIntoConstraints = NO;
40 redView.translatesAutoresizingMaskIntoConstraints = NO;
41 /**
42
43 NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),
44 NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),
45 NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),
46 NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),
47 NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),
48 NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),
49 NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),
50 NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),
51 NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline),
52 NSLayoutFormatAlignAllLastBaseline = NSLayoutFormatAlignAllBaseline,
53 NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = (1 << NSLayoutAttributeFirstBaseline),
54
55 NSLayoutFormatAlignmentMask = 0xFFFF,
56 */
57
58 // 添加水平方向blueView的約束
59 NSArray *HBlueViewConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:NSLayoutFormatAlignAllBottom metrics:nil views:@{@"blueView" : blueView}];
60 [self.view addConstraints:HBlueViewConstraint];
61
62 // 添加垂直方向的約束
63 NSArray *VConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView(50)]-20-[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}];
64 [self.view addConstraints:VConstraint];
65
66 #warning mark - VFL不支持運算符
67 // 添加redView水平方法的約束
68 // NSArray *HRedViewConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(==blueView)]" options:NSLayoutFormatAlignAllRight metrics:nil views:@{@"blueView" : blueView, @"redView" : redView}];
69 // [self.view addConstraints:HRedViewConstraint];
70
71 // 添加redView寬度等于blueView的寬度的一半
72 NSLayoutConstraint *redViewWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
73 [self.view addConstraint:redViewWidth];
74 }
75
76 @end
?
轉載于:https://www.cnblogs.com/DoNib-Coding/p/5014524.html
總結
- 上一篇: Android应用在不同版本间兼容性处理
- 下一篇: Thinking in Java,Fou