自动布局 - Masonry的使用(一)

1.Masonry支持的属性:

 //左侧
@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;

支持属性对应的NSLayoutAttrubute(枚举值) 从上往下依次和属性对应,

typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = 1,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,
NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,
NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),


NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),
NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

//表示没有属性
NSLayoutAttributeNotAnAttribute = 0 

};

2.Masonry的实际应用举例,以一个简单的居中视图为例

步骤1.
创建工程引入第三方Masonry并导入头文件Masonry.h , 我这里直接在ViewController.m里操作了截图如下:

导入头文件Masonry.h图

步骤2.
自然是上布局代码,代码每行都有清晰的注释,这里不做过多的介绍:
- (void)viewDidLoad {
[super viewDidLoad];
//防止block的循环引用

__weak typeof (self) weakSelf = self;
//初始化view 并设置背景
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];

//使用mas_makeConstraints添加约束
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    //添加大小约束
    make.size.mas_equalTo(CGSizeMake(200, 200));
    //添加居中的约束(居中的方式与self相同)
    make.center.equalTo(weakSelf.view);
}];
}

效果图

效果图

总结:简单介绍了Masonry和Masonry的简单实用,关于Masonry的其他使用的方法我会陆续发布文章.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,648评论 4 61
  • 第二天,张开朦胧的双眼第一件事就是拿起妈妈的手机配音,梳洗完去学作文。 下午学习,做作业、阅读、跳舞。 晚上,坚持...
    珍惜自己_84c3阅读 2,415评论 0 1
  • 伤心的时候别坐飞机 仿佛魂飞魄散落在天际 乘风归去到蓝白世界里 想要遇见记忆中的你 伤心的时候别坐飞机 背靠着你的...
    江东胡女阅读 2,854评论 6 2

友情链接更多精彩内容