当前位置: 首页 > java >正文

Masonry入门学习

Masonry

作用

轻量级布局框架,使用便捷的链式语法封装自动布局,简介明了,并且具有高可读性

在iOS开发中,如何布局UI是我们给用户良好体验的前提,在以往我们选择使用设置控件的frame来确定位置,简单的界面还好,但是对于复杂的界面如果再使用frame的话计算起来就很繁琐了,所以引入了自动布局(AutoLayout)来确定控件之间的位置,但是这种方式代码看起来太复杂了,可读性不高,如果想要了解的可以看笔者的这篇博客:AutoLayout自动布局

基本属性

//基本属性:
@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;

约束的使用

基础API

  • 使用时经常需要加mas_前缀,如果不想可以在引用masonry文件前加上下面的内容

//定义这个常量,就可以不用在开发过程中使用mas_前缀
#define MAS_SHORTHAND
//可以让Masonry帮我们自动把基础数据类型的数据,自动装箱为对象类型
#define MAS_SHORTHAND_GLOBALS
  • 添加约束的三种方法

//添加新约束
- (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;

Masonry是基于AutoLayout的实现计算视图的frame的

两种写法

  • mas_EqualTo():支持类型转换,支持复杂类型,是对equalTo的封装,使用的时候不需要指定约束边,默认取前面需要添加约束的边

  • equalTo:参数是对象类型,一般是视图对象活着mas_width、mas_height这样的坐标系对象

修饰语

Masonry为了让代码使用和阅读更容易理解,添加了and和with两个方法,只是将内部的self返回。

- (MASConstraint* )with {return self;
}

倍数

在Masonry中可以使用multipledBy设置一个视图的某个参数是另一视图某个参数的多少倍

 make.size.equalTo(self.view).multipliedBy(0.8);

insets

   make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>));

对应位置填入需要内嵌的便偏移量就行,不用区分正负

优先级

  • Priority可以指定具体的优先级(0 -- 1000)

  • priorityHigh:高优先级,值为750

  • priorityMedium:介于高和低之间(250 -- 750之间)

  • priorityLow:低优先级(值为250)

优先级写在链的最后

make.top.equalTo(self.view).with.priority(999);

使用示例

#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@property (nonatomic, strong)UIView* parentView;
@property (nonatomic, strong)UIView* childView;
@end
​
@implementation ViewController
​
- (void)viewDidLoad {[super viewDidLoad];[self createView];[self createButton];
}
​
- (void)createButton {UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];button.backgroundColor = [UIColor systemBlueColor];[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];[button setTitle:@"移动" forState:UIControlStateNormal];[self.view addSubview:button];[button mas_makeConstraints:^(MASConstraintMaker *make) {make.bottom.equalTo(self.view).offset(-200);make.left.equalTo(self.view).offset(100);make.width.height.mas_equalTo(60);}];[button addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];;
}
​
- (void)pressButton {[self.parentView mas_remakeConstraints:^(MASConstraintMaker *make) {make.width.height.mas_equalTo(180);}];
}
​
​
- (void)createView {self.parentView = [[UIView alloc] init];self.parentView.backgroundColor = [UIColor yellowColor];[self.view addSubview:self.parentView];[self.parentView mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(self.view).offset(40);make.left.equalTo(self.view).offset(40);make.height.width.mas_equalTo(300);}];self.childView = [[UIView alloc] init];self.childView.backgroundColor = [UIColor redColor];[self.parentView addSubview:self.childView];[self.childView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.parentView);make.top.equalTo(self.parentView);make.height.width.mas_equalTo(50);}];
}
​
@end

在这个demo里,我先使用了mas_make....方法为两个UIView添加了约束,在点击按钮后又使用mas_remake...方法更新了两个UIView的布局,通过上面的图片可以直观的看到效果

注意事项

  1. 在使用masonry前需要先将视图添加到对应的父视图上才行,否则程序会崩溃

  2. 注意使用mas_euqalTo和equalTo的区别

  3. 一个完整的约束需要包含位置、大小,缺一不可

http://www.xdnf.cn/news/19609.html

相关文章:

  • 精确率、召回率、漏检率、误判率
  • Git安装教程
  • AI瘦身狂魔!微软推出原生1-bit大模型,性能不减,内存仅需同行零头!
  • 基于大数据的京东手机销售数据 可视化分析设计与开发03446原创的定制程序,java、PHP、python、C#小程序、文案全套、毕设程序定制、成品等
  • 华清远见25072班I/O学习day2
  • 继承体系中的隐藏机制解析(继承中的作用域)
  • MongoDB主从切换实战:如何让指定从库“精准”升级为主库?保姆级教程!
  • 基于单片机智能家居语音控制系统
  • 如何在VS Code远程连接Xshell使用
  • Linux - Redis离线安装(安装包的方式安装Redis)
  • 从Redisson分布式锁看锁的设计思路
  • IPC 进程间通信 interprocess communicate
  • 企业微信AI落地:如何选择企业微信服务商?
  • Axios拦截器:前端通信的交通警察[特殊字符]
  • 搭载AX650N高能效比智能视觉芯片——AX2050系列边缘计算盒,可应用在智慧安防交通仓储教育,人脸识别,明厨亮灶,安全生产,智能机器人等
  • table表格字段明细展示
  • 不透明指针
  • 【iOS】折叠cell
  • 《青衣剑客 · Claude》连载
  • 总线矩阵的原理
  • 如何将多个Excel报表合并为一个汇总文件?
  • N32G43x Bootloader 中 ENV 区的管理与实现
  • 前缀和(优化算法)
  • ClickHouse常见问题——ClickHouseKeeper配置listen_host后不生效
  • 面试 TOP101 动态规划专题题解汇总Java版(BM62 —— BM82)
  • 二、SVN基础命令速查表
  • leetcode 1792. 最大平均通过率 中等
  • 通过 select into outfile / load data infile 进行数据导入导出学习笔记
  • 开源项目_金融分析工具TradingAgents
  • 01数据结构-红黑树