/
NSLayoutConstraint-代码实现自动布局的函数用法说明

NSLayoutConstraint-代码实现自动布局的函数用法说明

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://rainbownight.blog.51cto.com/1336585/1316181

 

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/NSLayoutAttributeRightNSLayoutAttributeLeading/NSLayoutAttributeTrailing的区别是left/right永远是指左右,而leading/trailing在某些从右至左习惯的地区会变成,leading是右边,trailing是左边。(大概是⊙﹏⊙b)

 

 

本文出自 “rainbownight” 博客,请务必保留此出处http://rainbownight.blog.51cto.com/1336585/1316181

Related content

UIEdgeInsets
UIEdgeInsets
More like this
iOS AutoLayout - get frame size width
iOS AutoLayout - get frame size width
More like this
谈谈iOS Animation
谈谈iOS Animation
More like this
Image Dimension
Image Dimension
More like this
NSMutableAttributedString
NSMutableAttributedString
More like this
Using OCLint in Xcode
Using OCLint in Xcode
More like this