首页 > 学院 > 开发设计 > 正文

Masonry约束

2019-11-09 18:12:24
字体:
来源:转载
供稿:网友
@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; //文本基线

**

属性有了,接着我们应该怎么在视图中添加约束呢,Masonry给我们提供了3个方法

**

//新增约束 - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;//更新约束 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;//清楚之前的所有约束,只会保留最新的约束 - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block; 合理的利用这个3个函数,基本上可以应对任何情况了2.居中一个view // 防止block中的循环引用 __weak typeof (self) weakSelf = self // 初始化一个View UIView *bgView = [[UIView alloc]init]; bgView.backgroundColor = [UIColor redColor]; [self.view addSubview:bgView]; // 使用mas_makeConstraints添加约束 [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(weakSelf.view); make.size.mas_equalTo(CGSizeMake(200, 200)); }];

是不是很简单,这里有一点要必须注意下,添加约束前必须要把view添加到视图上。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表