自动布局(AutoLayout)使用

/*1. 创建控件,并添加到当前的View中*/
self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];
[self.view addSubview:self.rightButton];

/*2.创建控件的X轴方向的约束 */
NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton
                                                                          attribute:NSLayoutAttributeCenterX
                                                                          relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                                             toItem:superview
                                                                          attribute:NSLayoutAttributeCenterX
                                                                         multiplier:1.0
                                                                           constant:-100.0f];
/*3. 创建控件的Y轴方向的约束 */
NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint constraintWithItem:self.rightButton
                                                                       attribute:NSLayoutAttributeCenterY
                                                                       relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                                          toItem:superview
                                                                       attribute:NSLayoutAttributeCenterY
                                                                      multiplier:1.0f
                                                                        constant:0.0f];
// 将约束条件添加到父View
[superview addConstraints:@[centerYMyConstraint, rightButtonXConstraint]];

上面的代码实现了控制 rightButton以默认大小,显示在superView的中心位置,水平方向偏左100.0。

这里主要用到了NSLayoutConstraint的:

constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c

的属性

view1:需要约束的视图

attr1:需要约束的位置(上下左右中心... )

relation:相关性(>=, = , <=,  一般用=就可以)

view2:目标视图(参照视图)

attr2: 参照位置

multiplier:参考值的倍数

c:偏移量


***注意***

self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;

这句要加上,不然约束无效。


约束宽高为固定值时,toItem 对象设为nil


方式二:

+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary *)views;

通过特定格式的文字描述设置约束

// Create constraints
childView.translatesAutoresizingMaskIntoConstraints = NO;
// 垂直方向间距
NSNumber *vPadding = @0;
// 水平方向间距
NSNumber *hPadding = @0;
// 将View转成视图字典(swift 没有该方法, 自己创建字典["childView":childView])
NSDictionary *views = NSDictionaryOfVariableBindings(childView);
// 设置数值字典
NSDictionary *metricsDictionary = NSDictionaryOfVariableBindings(vPadding, hPadding);
// 创建垂直方向的约束条件,format中的vPadding替换成metrics中的值
NSArray *vConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vPadding-[childView]-vPadding-|"
                                                                options:0
                                                                metrics:metricsDictionary
                                                                  views:views];
[self.view addConstraints:vConstraints];

参考文章

IOS自动布局

コードでAutolayout

你可能感兴趣的:(自动布局(AutoLayout)使用)