




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
iOS的Masonry介绍和使用一、背景 在iPhone3GS及其之前,window的尺寸都是320X480,简单开发即可; 出现iPhone4 4S出现Retina,但window是尺寸还是不变,继续简单开发; 出现iPhone5 5S,window的尺寸都是320X568,即高度变了,采用iOS提供的autoresizingMask, 可以简单适配,继续简单开发。出现iPhone6 6S及其Plus,window的width和height都发生各种变化,但屏幕比例保持不变,采用autolayout可以完成任务,但匹配比较麻烦。采用xib文件添加各种约束,或采用Visual Format Language (VFL)的编码方式可以完成基本的开发任务。二、Masonry的介绍GitHub的地址:/SnapKit/Masonry 轻量级框架,能以简单明了地实现页面布局。 链式语法,可读性高; view1 mas_makeConstraints:(MASConstraintMaker *make) make.edges.equalTo(superview).with.insets(padding); ; 可以理解为 make edges equalTo superview with insets padding 比xib文件添加各种限制和VFL语言更简洁,更有利于后续的维护和修改。三、Masonry的使用从GitHub下源码Masonry-master,添加到工程文件中。在pch文件加上#define MAS_SHORTHAND#define MAS_SHORTHAND_GLOBALS#import Masonry.h把examples的controller和view都导入到工程的对应的目录下。就可以开始学习。四、Masonry的知识点1、MASConstraintMakerinterface MASConstraintMaker : NSObject/*The following properties return a new MASViewConstraint with the first item set to the makers associated view and the appropriate MASViewAttribute */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; X向中心property (nonatomic, strong, readonly) MASConstraint *centerY; Y向中心property (nonatomic, strong, readonly) MASConstraint *baseline; 基线#if TARGET_OS_IPHONE | TARGET_OS_TVproperty (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;#endif2、三种尺寸关系equalTo = NSLayoutRelationEquallessThanOrEqualTo = NSLayoutRelationLessThanOrEqualgreaterThanOrEqualTo = NSLayoutRelationGreaterThanOrEqual3、参考例子Masonry会自动调用 view1.translatesAutoresizingMaskIntoConstraints=NO; 1) view.left大于等于label.left/these two constraints are exactly the samemake.left.greaterThanOrEqualTo(label);make.left.greaterThanOrEqualTo(label.mas_left);2) 设置width或height的最大值和最小值/width = 200 & width = 400make.width.greaterThanOrEqualTo(200);make.width.lessThanOrEqualTo(400)3) 属性left、right、CenterY等对齐属性不能设置固定值,但可以设置和superview的相对值/creates view.left = view.superview.left + 10make.left.lessThanOrEqualTo(10)make.top.mas_equalTo(42);make.height.mas_equalTo(20);make.size.mas_equalTo(CGSizeMake(50, 100);make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0);make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0);4) 可以使用数据组的混合值make.height.equalTo(view1.mas_height, view2.mas_height);make.height.equalTo(view1, view2);make.left.equalTo(view1, 100, view3.right);4、设置优先级 低、中、高和具体值priority allows you to specify an exact prioritypriorityHigh equivalent to UILayoutPriorityDefaultHighpriorityMedium is half way between high and lowpriorityLow equivalent to UILayoutPriorityDefaultLow例如:make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();make.top.equalTo(label.mas_top).with.priority(600);-(MASConstraint*)withreturnself;-(MASConstraint*)andreturnself;5、约束组合edges/ make top, left, bottom, right equal view2make.edges.equalTo(view2);/ make top = superview.top + 5, left = superview.left + 10,/ bottom = superview.bottom - 15, right = superview.right - 20make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20)size/ make width and height greater than or equal to titleLabelmake.size.greaterThanOrEqualTo(titleLabel)/ make width = superview.width + 100, height = superview.height - 50make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50)center/ make centerX and centerY = button1make.center.equalTo(button1)/make centerX=superview.centerX - 5, centerY=superview.centerY + 10make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10)使用属性链来提高可读性/All edges but the top should equal those of the superviewmake.left.right.and.bottom.equalTo(superview);make.top.equalTo(otherView);6、如何使用1) 使用引用/ in public/private interfaceproperty (nonatomic, strong) MASConstraint *topConstraint;./ when making constraintsview1 mas_makeConstraints:(MASConstraintMaker *make) self.topConstraint = make.top.equalTo(superview.mas_top).with.offset(padding.top); make.left.equalTo(superview.mas_left).with.offset(padding.left);./ then later you can callself.topConstraint uninstall;2) 使用makeConstraints、updateConstraints和remakeConstraints-(NSArray *)mas_makeConstraints: (void()(MASConstraintMaker*make)block;-(NSArray*)mas_updateConstraints: (void()(MASConstraintMaker*make)block-(NSArray*)mas_remakeConstraints: (void()(MASConstraintMaker*make)blockmas_makeConstraints只负责新增约束Autol
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度医药中间体生产及全球分销合同
- 2025年可持续草原资源开发与民族文化传承合作协议
- 2025布艺设计师职业培训及市场拓展服务合同
- 2025年四川特色火锅原材料供应合同:绿色食材版
- 2025年跨境电商保证金支付与担保一体化服务合同
- 2025年智能家居系统研发与应用合同
- 2025年环保材料贸易居间服务专项合同模板
- 2025年农村合作银行农户小额贷款合同样本
- 2025年生态环境质量综合评价技术服务合同
- 2025-2030医疗美容药物监管政策与市场规范化报告
- 民族文化宫2025年公开招聘17人笔试模拟试题含答案详解
- 2025年幼儿园教师专业考试试题及答案书
- 2025秋新部编版一年级上册语文教学计划+教学进度表
- 2025年国家公务员考试行测真题及答案(完整版)
- 小型企业网络构建:VPN设置与配置详解
- 消化道内异物疑难病例讨论
- 2025年预防接种技能竞赛征集试题
- 道路运输安全生产法律法规有哪些
- 年度述职活动方案
- 抗衰老培训课件
- 肿瘤科讲课课件
评论
0/150
提交评论