以前一直使用第三方库Masonry和SDAutoLayout,最近没事看了一下原生的约束,发现也很方便。NSLayoutAnchor作为iOS9的新特性,是对Auto Layout体系的完善和补充。
A factory class for creating layout constraint objects using a fluent API.
下面是它的属性
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *leadingAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *trailingAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *leftAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *rightAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutYAxisAnchor *topAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutYAxisAnchor *bottomAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutDimension *widthAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutDimension *heightAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutXAxisAnchor *centerXAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutYAxisAnchor *centerYAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutYAxisAnchor *firstBaselineAnchor NS_AVAILABLE_IOS(9_0);
@property(nonatomic,readonly,strong) NSLayoutYAxisAnchor *lastBaselineAnchor NS_AVAILABLE_IOS(9_0);
大家只要看见它的属性,就知道它们的含义了,这里不做过多解释,直接上例子
例子1
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = 0;
[self.view addSubview:titleLabel];
titleLabel.translatesAutoresizingMaskIntoConstraints = false;//关闭frame确定位置,开启autolayout布局
[titleLabel.topAnchor constraintEqualToAnchor:self.viewtopAnchor constant:0].active = true;
[titleLabel.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:1];
[titleLabel.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:12].active = true;
[titleLabel.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-12].active = true;
[titleLabel.heightAnchor constraintEqualToConstant:99].active = true;
例子2
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = 0;
[self.view addSubview:titleLabel];
titleLabel.translatesAutoresizingMaskIntoConstraints = false;
NSLayoutConstraint *btnAnimate_bottom = [titleLabel.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:1];
NSLayoutConstraint *btnAnimate_top = [titleLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:0];
NSLayoutConstraint *btnAnimate_left = [titleLabel.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:12];
NSLayoutConstraint *btnAnimate_right = [titleLabel.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-12];
[NSLayoutConstraint activateConstraints:@[btnAnimate_bottom,btnAnimate_top,btnAnimate_left,btnAnimate_right]];
例子2的约束方式比例子1更有效率,推荐第二种约束方式。
下面是官方API对activateConstraints的描述
This convenience method provides an easy way to activate a set of constraints with one call. The effect of this method is the same as setting the active property of each constraint to YES. Typically, using this method is more efficient than activating each constraint individually.
activateConstraints ->激活约束
deactivateConstraints->禁用约束
***需要注意是leaddingAnchor和leftAnchor,rightAnchor和trailingAnchor不能混用,下面是官方API的解释。
While the NSLayoutAnchor class provides additional type checking, it is still possible to create invalid constraints. For example, the compiler allows you to constrain one view’s leadingAnchor with another view’s leftAnchor, since they are both NSLayoutXAxisAnchor instances. However, Auto Layout does not allow constraints that mix leading and trailing attributes with left or right attributes. As a result, this constraint crashes at runtime.