版本:1.1.0
View+MASAdditions.h
@interface MAS_VIEW (MASAdditions)
@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
// mas_right === mas_attribute(NSLayoutAttributeRight)
@property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr);
例:
make.left.equalTo(redView.mas_attribute(NSLayoutAttributeRight)).offset(15);
等同于
make.left.equalTo(redView.mas_right).offset(15);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline;
#if TARGET_OS_IPHONE || TARGET_OS_TV
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins;
//iOS11的safeArea
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeading NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTrailing NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideWidth NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideHeight NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterX NS_AVAILABLE_IOS(11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideCenterY NS_AVAILABLE_IOS(11.0);
#endif
//标志符
@property (nonatomic, strong) id mas_key;
//返回此视图和另一个视图之间最接近的公共父视图
- (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view;
例:
[self addSubview:redView];
[self addSubview:orangeView];
[redView mas_closestCommonSuperview:orangeView] 会返回self
// 所有定义的约束都会添加到视图或适当的超级视图中
- (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;
//所有定义的约束将添加到视图或适当的超级视图。
//如果存在现有约束,则将对其进行更新。
- (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;
//所有定义的约束将添加到视图或适当的超级视图。
//先前为该视图安装的所有约束将被删除。
- (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;
@end
例:
[orangeView mas_makeConstraints:^(MASConstraintMaker *make) {
}];
MASConstraintMaker.h
@interface MASConstraintMaker : NSObject
@property (nonatomic, strong, readonly) MASConstraint *left;
@property (nonatomic, strong, readonly) MASConstraint *top;
@property (nonatomic, strong, readonly) MASConstraint *right;
@property (nonatomic, strong, readonly) MASConstraint *bottom;
@property (nonatomic, strong, readonly) MASConstraint *leading;
@property (nonatomic, strong, readonly) MASConstraint *trailing;
@property (nonatomic, strong, readonly) MASConstraint *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
@property (nonatomic, strong, readonly) MASConstraint *firstBaseline;
@property (nonatomic, strong, readonly) MASConstraint *lastBaseline;
#if TARGET_OS_IPHONE || TARGET_OS_TV
@property (nonatomic, strong, readonly) MASConstraint *leftMargin;
@property (nonatomic, strong, readonly) MASConstraint *rightMargin;
@property (nonatomic, strong, readonly) MASConstraint *topMargin;
@property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
@property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
@property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;
#endif
typedef NS_OPTIONS(NSInteger, MASAttribute) {
MASAttributeLeft = 1 << NSLayoutAttributeLeft,
MASAttributeRight = 1 << NSLayoutAttributeRight,
MASAttributeTop = 1 << NSLayoutAttributeTop,
MASAttributeBottom = 1 << NSLayoutAttributeBottom,
MASAttributeLeading = 1 << NSLayoutAttributeLeading,
MASAttributeTrailing = 1 << NSLayoutAttributeTrailing,
MASAttributeWidth = 1 << NSLayoutAttributeWidth,
MASAttributeHeight = 1 << NSLayoutAttributeHeight,
MASAttributeCenterX = 1 << NSLayoutAttributeCenterX,
MASAttributeCenterY = 1 << NSLayoutAttributeCenterY,
MASAttributeBaseline = 1 << NSLayoutAttributeBaseline,
MASAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline,
MASAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline,
#if TARGET_OS_IPHONE || TARGET_OS_TV
MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin,
MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin,
MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin,
MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin,
MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin,
MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin,
MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins,
MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins,
#endif
};
//同时添加多个约束
@property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs);
例:
make.width.height.equalTo(@(100));
等同于
make.attributes(MASAttributeWidth|MASAttributeHeight).equalTo(@(100));
//添加边距 相同于attributes(MASAttributeTop|MASAttributeLeft|MASAttributeBottom|MASAttributeRight)
@property (nonatomic, strong, readonly) MASConstraint *edges;
例:
make.edges.equalTo(self).inset(150);
等同于
make.attributes(MASAttributeLeft|MASAttributeRight|MASAttributeTop|MASAttributeBottom).equalTo(self).inset(150);
等同于
make.top.left.bottom.right.equalTo(self).inset(150);
//添加大小 相同于attributes(MASAttributeWidth|MASAttributeHeight)
@property (nonatomic, strong, readonly) MASConstraint *size;
例:
make.size.equalTo(@(100));
等同于
make.attributes(MASAttributeWidth|MASAttributeHeight).equalTo(@(100));
等同于
make.width.height.equalTo(@(100));
//添加中心 相同于attributes(MASAttributeCenterX|MASAttributeCenterY)
@property (nonatomic, strong, readonly) MASConstraint *center;
例:
make.center.equalTo(self);
等同于
make.attributes(MASAttributeCenterX|MASAttributeCenterY).equalTo(self);
等同于
make.centerX.centerY.equalTo(self);
//表示此时为约束更新状态
@property (nonatomic, assign) BOOL updateExisting;
//表示现在为约束重置状态
@property (nonatomic, assign) BOOL removeExisting;
例:
[greenView mas_updateConstraints:^(MASConstraintMaker *make) {
}];
等同于
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.updateExisting = YES;
}];
[greenView mas_remakeConstraints:^(MASConstraintMaker *make) {
}];
等同于
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.removeExisting = YES;
}];
//初始化
- (id)initWithView:(MAS_VIEW *)view;
//使所有约束生效
- (NSArray *)install;
例:
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(15);
make.top.equalTo(redView.mas_bottom).offset(15);
make.width.equalTo(@(200));
make.height.equalTo(@(70));
}];
等同于
blueView.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *make = [[MASConstraintMaker alloc] initWithView:blueView];
make.left.equalTo(self).offset(15);
make.top.equalTo(redView.mas_bottom).offset(15);
make.width.equalTo(@(200));
make.height.equalTo(@(70));
[make install];
- (MASConstraint * (^)(dispatch_block_t))group;
@end
NSArray+MASAdditions.h
@interface NSArray (MASAdditions)
//为数组里面的元素(必须为UIView或子类)添加约束
- (NSArray *)mas_makeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block;
例:
UIView *cyanView = [[UIView alloc] init];
cyanView.backgroundColor = [UIColor cyanColor];
[self addSubview:cyanView];
UIView *purpleView = [[UIView alloc] init];
purpleView.backgroundColor = [UIColor purpleColor];
[self addSubview:purpleView];
//两个view共同的约束可以这样来实现
[@[cyanView, purpleView] mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self).offset(15);
make.width.equalTo(@(100));
make.height.equalTo(@(30));
}];
[cyanView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(blueView.mas_bottom).equalTo(15);
}];
[purpleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(cyanView.mas_bottom).offset(15);
}];
效果如下图
image.png
typedef NS_ENUM(NSUInteger, MASAxisType) {
MASAxisTypeHorizontal, //水平
MASAxisTypeVertical //竖直
};
//将数组里的view排成一排或一列 fixedSpacing间距 leadSpacing前(上)边距 tailSpacing后(下)边距
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing
leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
例:
UIView *magentaView0 = [[UIView alloc] init];
magentaView0.backgroundColor = [UIColor magentaColor];
UIView *magentaView1 = [[UIView alloc] init];
magentaView1.backgroundColor = [UIColor magentaColor];
UIView *magentaView2 = [[UIView alloc] init];
magentaView2.backgroundColor = [UIColor magentaColor];
[greenView addSubview:magentaView1];
[greenView addSubview:magentaView0];
[greenView addSubview:magentaView2];
[@[magentaView0, magentaView1, magentaView2] mas_distributeViewsAlongAxis:MASAxisTypeVertical
withFixedSpacing:5 leadSpacing:10 tailSpacing:15];
//因为是MASAxisTypeVertical垂直而且上下边距和间距已经确定,所有高度也也确定,因而下面只需要添加左右方向的约束
[@[magentaView0, magentaView1, magentaView2] makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(15);
make.width.equalTo(@(50));
}];
效果如下图
image.png
//将数组里的view排成一排或一列 fixedItemLength为每个view的宽度(高度)
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength
leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;
@end
例:
[blueView addSubview:magentaView1];
[blueView addSubview:magentaView0];
[blueView addSubview:magentaView2];
[@[magentaView0, magentaView1, magentaView2] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
withFixedItemLength:50 leadSpacing:5 tailSpacing:10];
//因为是MASAxisTypeHorizontal水平而且左右边距和宽度已经确定,所有每个view的间距也能确定,因而下面只需要添加上下方向的约束
[@[magentaView0, magentaView1, magentaView2] makeConstraints:^(MASConstraintMaker *make) {
make.top.offset(10);
make.height.equalTo(@(50));
}];
效果如下图
image.png
MASConstraint.h
@interface MASConstraint : NSObject
//边距赋值 只对以下属性生效
//NSLayoutAttributeTop = insets.top, NSLayoutAttributeLeft = insets.left,
//NSLayoutAttributeBottom = insets.bottom, NSLayoutAttributeRight = insets.right
- (MASConstraint * (^)(MASEdgeInsets insets))insets;
例:
make.edges.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
等同于
make.top.left.bottom.right.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
等同于
make.top.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
make.left.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
make.bottom.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
make.right.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
//边距赋值 只对以下属性生效
//NSLayoutAttributeTop NSLayoutAttributeLeft NSLayoutAttributeBottom NSLayoutAttributeRight = inset
- (MASConstraint * (^)(CGFloat inset))inset;
例:
make.edges.equalTo(yellowView).inset(5);
等同于
make.edges.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
//大小赋值 只对以下属性生效
//NSLayoutAttributeWidth = sizeOffset.with, NSLayoutAttributeHeight = sizeOffset.height
- (MASConstraint * (^)(CGSize offset))sizeOffset;
例:
make.size.sizeOffset(CGSizeMake(50, 100));
等同于
make.width.height.sizeOffset(CGSizeMake(50, 100));
等同于
make.width.sizeOffset(CGSizeMake(50, 100));
make.height.sizeOffset(CGSizeMake(50, 100));
//中心赋值 只对以下属性生效
//NSLayoutAttributeCenterX = centerOffset.x, NSLayoutAttributeCenterY = centerOffset.y
- (MASConstraint * (^)(CGPoint offset))centerOffset;
例:
make.center.centerOffset(CGPointMake(100, -150));
等同于
make.centerX.centerY.centerOffset(CGPointMake(100, -150));
等同于
make.centerX.centerOffset(CGPointMake(100, -150));
make.centerY.centerOffset(CGPointMake(100, -150));
//给约束添加常数 相当于给NSLayoutConstraint添加constant
//+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1
//relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2
//multiplier:(CGFloat)multiplier constant:(CGFloat)c
- (MASConstraint * (^)(CGFloat offset))offset;
//可使用NSNumber UIEdgeInsets CGPoint CGSize的NSValue对象
//效果同offset insets centerOffset sizeOffset
- (MASConstraint * (^)(NSValue *value))valueOffset;
例:insets
make.edges.equalTo(yellowView).valueOffset([NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(5, 10, 15, 20)]);
等同于
make.edges.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
例:centerOffset
make.center.equalTo([NSValue valueWithCGPoint:CGPointMake(100, -150)]);
等同于
make.center.centerOffset(CGPointMake(100, -150));
例:sizeOffset
make.size.valueOffset([NSValue valueWithCGSize:CGSizeMake(50, 100)]);
等同于
make.size.sizeOffset(CGSizeMake(50, 100));
例:offset
make.left.equalTo(yellowView).offset(5);
等同于
make.left.equalTo(yellowView).sizeOffset([NSNumber numberWithInteger:5]);
//给约束添加乘数 相当于给NSLayoutConstraint添加multiplier
//+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1
//relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2
//multiplier:(CGFloat)multiplier constant:(CGFloat)c
- (MASConstraint * (^)(CGFloat multiplier))multipliedBy;
//给约束添加乘数 相当于给NSLayoutConstraint添加multiplier multiplier = 1/dividedBy
//+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1
//relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2
//multiplier:(CGFloat)multiplier constant:(CGFloat)c
- (MASConstraint * (^)(CGFloat divider))dividedBy;
例:
//width = height * 0.5
make.width.equalTo(grayView.mas_height).with.multipliedBy(0.5);
make.width.equalTo(50);
相当于
//width = height / 2
make.width.equalTo(grayView.mas_height).with.dividedBy(2);
make.width.equalTo(50);
typedef UILayoutPriority MASLayoutPriority;
static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired;
static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh;
static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500;
static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow;
static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel;
//优先级 MASLayoutConstraint.priority
- (MASConstraint * (^)(MASLayoutPriority priority))priority;
//MASLayoutPriorityDefaultLow
- (MASConstraint * (^)(void))priorityLow;
//MASLayoutPriorityMedium
- (MASConstraint * (^)(void))priorityMedium;
//MASLayoutPriorityHigh
- (MASConstraint * (^)(void))priorityHigh;
//给约束添加关系 相当于给NSLayoutConstraint添加relation
//+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1
//relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2
//multiplier:(CGFloat)multiplier constant:(CGFloat)c
//添加NSLayoutRelationEqual关系
//可传入 MASViewAttribute = view.mas_left
//UIView = view,
//NSValue = @(10),[NSValue valueWithCGPoint:CGPointMake(100, -150)]
//NSArray 元素为上面的三种,要注意逻辑关系
- (MASConstraint * (^)(id attr))equalTo;
例:
make.left.equalTo(self.mas_left).offset(15);
make.left.equalTo(self).offset(15);
make.left.equalTo(@15);
make.left.equalTo(@[grayView, brownView]);
//添加NSLayoutRelationGreaterThanOrEqual关系
//可传入的类型如equalTo
- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;
////添加NSLayoutRelationLessThanOrEqual关系
//可传入的类型如equalTo
- (MASConstraint * (^)(id attr))lessThanOrEqualTo;
//返回本身 增加代码可读性
- (MASConstraint *)with;
- (MASConstraint *)and;
例:
make.left.right.equalTo(self).offset(15);
等同于
make.left.and.right.equalTo(self).with.offset(15);
//连续添加约束
//make.left.right.top.bottom.equalTo(self)
//其中第一个约束left = MASConstraintMaker.left
//后面的所有约束都是下面的约束
- (MASConstraint *)left;
- (MASConstraint *)top;
- (MASConstraint *)right;
- (MASConstraint *)bottom;
- (MASConstraint *)leading;
- (MASConstraint *)trailing;
- (MASConstraint *)width;
- (MASConstraint *)height;
- (MASConstraint *)centerX;
- (MASConstraint *)centerY;
- (MASConstraint *)baseline;
- (MASConstraint *)firstBaseline;
- (MASConstraint *)lastBaseline;
#if TARGET_OS_IPHONE || TARGET_OS_TV
- (MASConstraint *)leftMargin;
- (MASConstraint *)rightMargin;
- (MASConstraint *)topMargin;
- (MASConstraint *)bottomMargin;
- (MASConstraint *)leadingMargin;
- (MASConstraint *)trailingMargin;
- (MASConstraint *)centerXWithinMargins;
- (MASConstraint *)centerYWithinMargins;
#endif
//标志符
- (MASConstraint * (^)(id key))key;
//效果同.insets
- (void)setInsets:(MASEdgeInsets)insets;
//效果同.inset
- (void)setInset:(CGFloat)inset;
//效果同.sizeOffset
- (void)setSizeOffset:(CGSize)sizeOffset;
//效果同.centerOffset
- (void)setCenterOffset:(CGPoint)centerOffset;
//效果同.offset
- (void)setOffset:(CGFloat)offset;
例:
[make.edges.equalTo(yellowView) setInsets:UIEdgeInsetsMake(5, 10, 15, 20)];
等同于
make.edges.equalTo(yellowView).insets(UIEdgeInsetsMake(5, 10, 15, 20));
#if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
// macOS专用属性 暂不处理
@property (nonatomic, copy, readonly) MASConstraint *animator;
#endif
//让约束生效
- (void)activate;
//让约束失效
- (void)deactivate;
//效果同activate
- (void)install;
//效果同deactivate
- (void)uninstall;
例:
blueView.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *make = [[MASConstraintMaker alloc] initWithView:blueView];
make.left.equalTo(self).offset(15);
make.top.equalTo(redView.mas_bottom).offset(15);
make.width.equalTo(@(200));
make.height.equalTo(@(70));
[make install];
等同于
blueView.translatesAutoresizingMaskIntoConstraints = NO;
MASConstraintMaker *make = [[MASConstraintMaker alloc] initWithView:blueView];
[make.left.equalTo(self).offset(15) activate];
[make.top.equalTo(redView.mas_bottom).offset(15) activate];
[make.width.equalTo(@(200)) install];
[make.height.equalTo(@(70)) install];
@end
@interface MASConstraint (AutoboxingSupport)
//效果同equalTo greaterThanOrEqualTo lessThanOrEqualTo
//但多支持CGFloat类型的参数
- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
//已弃用
- (MASConstraint * (^)(id offset))mas_offset;
例:
make.left.mas_equalTo(15);
等同于
make.left.equalTo(@15);
@end
MASViewConstraint.h
//内部使用 仅介绍
@interface MASViewConstraint : MASConstraint <NSCopying>
//约束的第一条属性
@property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute;
//约束的第二条属性
//MASConstraint.equalTo greaterThanOrEqualTo lessThanOrEqualTo传入的参数
//类型为 MASViewAttribute, UIView, NSValue, NSArray
@property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute;
//通过第一条属性初始化
- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute;
//返回view(NSLayoutAttribute的第一个对象)的全部已生效的约束(MASViewConstraints)
+ (NSArray *)installedConstraintsForView:(MAS_VIEW *)view;
@end
MASViewAttribute.h
@interface MASViewAttribute : NSObject
//NSLayoutAttribute的所属的视图 readonly
@property (nonatomic, weak, readonly) MAS_VIEW *view;
////+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1
//relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2
//multiplier:(CGFloat)multiplier constant:(CGFloat)c
//NSLayoutAttribute的item
//用于上面方法的view1 view2 readonly
@property (nonatomic, weak, readonly) id item;
//布局属性 readonly
@property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute;
//初始化 此时item默为view
- (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute;
//初始化
- (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute;
//判断当前的布局属性是否为NSLayoutAttributeWidth、NSLayoutAttributeHeight
- (BOOL)isSizeAttribute;
@end
View+MASShorthandAdditions.h
//去掉所有mas_
@property (nonatomic, strong, readonly) MASViewAttribute *left;
@property (nonatomic, strong, readonly) MASViewAttribute *top;
......
- (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block;
例:
[grayView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(5);
}];
等同于
[grayView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.left).offset(5);
}];
NSArray+MASShorthandAdditions.h
- (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block;
等同于
- (NSArray *)mas_makeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void (NS_NOESCAPE ^)(MASConstraintMaker *make))block;
如何做动画效果
例1:
- (instancetype)init {
self = [super init];
if (self) {
[self addSubview:redView];
[redView makeConstraints:^(MASConstraintMaker *make) {
//约束
}];
}
return self;
}
//为yes时 初始化后会先调用一次updateConstraints
+ (BOOL)requiresConstraintBasedLayout {
return NO;
}
- (void)updateConstraints {
[redView updateConstraints:^(MASConstraintMaker *make) {
//要更新的约束
}];
//[redView remakeConstraints:^(MASConstraintMaker *make) {
// //新约束
//}];
[super updateConstraints];
}
- (void)action {
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}
例2:
- (instancetype)init {
self = [super init];
if (self) {
[self addSubview:redView];
}
return self;
}
//为yes时 初始化后会先调用一次updateConstraints 就不用在init时添加约束了
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
- (void)updateConstraints {
[redView updateConstraints:^(MASConstraintMaker *make) {
//要更新的约束
}];
//[redView remakeConstraints:^(MASConstraintMaker *make) {
// //新约束
//}];
[super updateConstraints];
}
- (void)action {
[self setNeedsUpdateConstraints];
[self updateConstraintsIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}
例3:
- (instancetype)init {
self = [super init];
if (self) {
[self addSubview:redView];
[redView makeConstraints:^(MASConstraintMaker *make) {
//约束
}];
}
return self;
}
- (void)action {
[UIView animateWithDuration:0.4 animations:^{
[redView updateConstraints:^(MASConstraintMaker *make) {
//要更新的约束
}];
//[redView remakeConstraints:^(MASConstraintMaker *make) {
// //新约束
//}];
[self layoutIfNeeded];
}];
}
网友评论