iOS - NSLayoutConstraint

 

1
2
3
4
5
6
7
[NSLayoutConstraint constraintWithItem:(id)item
                             attribute:(NSLayoutAttribute)attribute
                             relatedBy:(NSLayoutRelation)relation
                                toItem:(id)otherItem
                             attribute:(NSLayoutAttribute)otherAttribute
                            multiplier:(CGFloat)multiplier
                              constant:(CGFloat)constant]

 

 

参数说明:

第一个参数:指定约束左边的视图view1

第二个参数:指定view1的属性attr1,具体属性见文末。

第三个参数:指定左右两边的视图的关系relation,具体关系见文末。

第四个参数:指定约束右边的视图view2

第五个参数:指定view2的属性attr2,具体属性见文末。

第六个参数:指定一个与view2属性相乘的乘数multiplier

第七个参数:指定一个与view2属性相加的浮点数constant

 

这个函数的对照公式为:

view1.attr1 <relation> view2.attr2 * multiplier + constant

 

注意:

1.如果你想设置的约束里不需要第二个view,要将第四个参数设为nil,第五个参数设为NSLayoutAttributeNotAnAttribute

 

举例:

1
2
3
4
5
6
7
[NSLayoutConstraint constraintWithItem:view1
                             attribute:NSLayoutAttributeLeft
                             relatedBy:NSLayoutRelationEqual
                                toItem:view2
                             attribute:NSLayoutAttributeRight
                            multiplier:1
                              constant:10]


翻译过来就是:view1的左侧,在,view2的右侧,再多10个点,的地方。

 

附视图的属性和关系的值:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
typedef NS_ENUM(NSInteger, NSLayoutRelation) {
    NSLayoutRelationLessThanOrEqual = -1,          //小于等于
    NSLayoutRelationEqual = 0,                     //等于
    NSLayoutRelationGreaterThanOrEqual = 1,        //大于等于
};
typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
    NSLayoutAttributeLeft = 1,                     //左侧
    NSLayoutAttributeRight,                        //右侧
    NSLayoutAttributeTop,                          //上方
    NSLayoutAttributeBottom,                       //下方
    NSLayoutAttributeLeading,                      //首部
    NSLayoutAttributeTrailing,                     //尾部
    NSLayoutAttributeWidth,                        //宽度
    NSLayoutAttributeHeight,                       //高度
    NSLayoutAttributeCenterX,                      //X轴中心
    NSLayoutAttributeCenterY,                      //Y轴中心
    NSLayoutAttributeBaseline,                     //文本底标线
                                                                                                                                                    
    NSLayoutAttributeNotAnAttribute = 0            //没有属性
};

 

NSLayoutAttributeLeft/NSLayoutAttributeRight 和NSLayoutAttributeLeading/NSLayoutAttributeTrailing的区别是left/right永远是指左右,而leading/trailing在某些从右至左习惯的地区会变成,leading是右边,trailing是左边。

 

示例代码:

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3 
 4 //  创建控件
 5     UIView *blueView = [UIView new];
 6     blueView.backgroundColor = [UIColor blueColor];
 7     [self.view addSubview:blueView];
 8     
 9     UIView *redView = [UIView new];
10     redView.backgroundColor = [UIColor redColor];
11     [self.view addSubview:redView];
12     
13     /*
14      NSLayoutAttributeLeft = 1, 左边
15      NSLayoutAttributeRight,    右边
16      NSLayoutAttributeTop,      顶部
17      NSLayoutAttributeBottom,   底部
18      NSLayoutAttributeLeading,  左边
19      NSLayoutAttributeTrailing,  右边
20      NSLayoutAttributeWidth,     宽
21      NSLayoutAttributeHeight,    高
22      NSLayoutAttributeCenterX,   水平中线
23      NSLayoutAttributeCenterY,   竖直中线
24      
25      */
26      /*
27       typedef NS_ENUM(NSInteger, NSLayoutRelation) {
28       NSLayoutRelationLessThanOrEqual = -1,
29       NSLayoutRelationEqual = 0,
30       NSLayoutRelationGreaterThanOrEqual = 1,
31       };
32       */
33 //  前提,给那个控件添加约束,就把那个控件的autoresing关闭
34     blueView.translatesAutoresizingMaskIntoConstraints = NO;
35     redView.translatesAutoresizingMaskIntoConstraints = NO;
36 //    self.view.translatesAutoresizingMaskIntoConstraints = NO;
37 /*
38   使用autolayout的两个前提:
39   1.一定要添加到控制器的view的层次结构中
40   2.关闭要添加约束的控件的autoresing
41      translatesAutoresizingMaskIntoConstraints
42  */
43     
44     
45 
46 // blueView的顶部约束
47    NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:20];
48     [self.view addConstraint:blueTop];
49  
50     // blueView的左边约束
51     NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:20];
52     [self.view addConstraint:blueLeft];
53     
54     
55     // blueView的右边约束
56     NSLayoutConstraint *blueRigth = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-20];
57     [self.view addConstraint:blueRigth];
58     
59     // blueView的高度
60     NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100];
61    
62     [blueView addConstraint:blueHeight];
63     
64     
65     // redView的顶部约束
66     NSLayoutConstraint *redTop = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1 constant:20];
67     [self.view addConstraint:redTop];
68     
69     // redView右对齐
70     NSLayoutConstraint *redRight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
71     [self.view addConstraint:redRight];
72     
73     // redView高
74     NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
75     [self.view addConstraint:redHeight];
76     
77     // redView宽
78     NSLayoutConstraint *redWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
79     [self.view addConstraint:redWidth];
80 }
81 
82 
83 
84 @end

 

posted @ 2015-06-09 11:48  Qingyun_Qearl  阅读(516)  评论(0编辑  收藏  举报