【iOS开发】Mansory自动布局(autolayout)使用教程
来源:互联网 发布:微派网络 融资 编辑:程序博客网 时间:2024/06/05 18:22
一、简介
Mansory
Mansory是一个轻量级的自动布局库,采用独特的链式语法进行代码封装,具有高可用性和阅读性等特点,支持iOS和Mac OS X。
先上点官方的代码,当当开胃菜。
Heres the same constraints created using MASConstraintMaker。
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);[view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler make.left.equalTo(superview.mas_left).with.offset(padding.left); make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom); make.right.equalTo(superview.mas_right).with.offset(-padding.right);}];
Or even shorter
[view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(superview).with.insets(padding);}];
上面两段代码是等价的,让一个view的各个边缘距离父视图分别为10的间距。实际效果是一样的,只是方法上面有优略而已。
二、实践
看mansory里面的view支持哪些属性
下面通过一些Demo来进一步介绍如何使用mansory。
1.【初阶】居中显示一个UIView
- (void)viewDidLoad{ [super viewDidLoad]; UIView *t_view = [UIView new]; t_view.backgroundColor = [UIColor redColor]; // 记得在添加约束之前,一定要先view添加到superView上面,否则会出现崩溃 [self.view addSubview:t_view]; [t_view mas_makeConstraints:^(MASConstraintMaker *make) { // t_view居中 make.center.equalTo(self.view); // t_view的size make.size.mas_equalTo(CGSizeMake(300, 300)); }];}
2.【初阶】让一个view略小于其superView(边距为20)
- (void)viewDidLoad{ [super viewDidLoad]; UIView *t_view2 = [UIView new]; t_view2.backgroundColor = [UIColor greenColor]; [self.view addSubview:t_view2]; [t_view2 mas_makeConstraints:^(MASConstraintMaker *make) {// 等价于 //make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(20, 20, 20, 20));// 等价于 //make.top.bottom.left.and.right.equalTo(self.view).with.insets(UIEdgeInsetsMake(20, 20, 20, 20)); make.top.equalTo(self.view).with.offset(20); make.bottom.equalTo(self.view).with.offset(-20); make.left.equalTo(self.view).with.offset(20); make.right.equalTo(self.view).with.offset(-20); }];}
3.【初阶】让两个宽度为200的view 垂直居中且等宽等间距的排列 (自动计算高度)
- (void)viewDidLoad{ [super viewDidLoad]; int padding = 30; UIView *t_view3 = [UIView new]; t_view3.backgroundColor = [UIColor blueColor]; [self.view addSubview:t_view3]; UIView *t_view4 = [UIView new]; t_view4.backgroundColor = [UIColor orangeColor]; [self.view addSubview:t_view4]; [t_view3 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view.mas_centerX); make.top.equalTo(self.view.mas_top).with.offset(padding); make.bottom.equalTo(t_view4.mas_top).with.offset(-padding); make.height.equalTo(t_view4); make.width.mas_equalTo(@200); }]; [t_view4 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view.mas_centerX); make.top.equalTo(t_view3.mas_bottom).with.offset(padding); make.bottom.equalTo(self.view.mas_bottom).with.offset(-padding); make.height.equalTo(t_view3); make.width.mas_equalTo(@200); }];}
4.【中阶】在UIScrollView中顺序排列一些view并自动计算contentSize
注意:scrollView计算contentSize的时候,要先用一个containView填满整个scrollView,这样约束才能够准确计算。
- (void)viewDidLoad{ [super viewDidLoad]; UIScrollView *scrollView = [UIScrollView new]; scrollView.backgroundColor = [UIColor grayColor]; [self.view addSubview:scrollView]; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(20, 20, 20, 20)); }]; // 这个containView是用来先填充整个scrollView的,到时候这个containView的size就是scrollView的contentSize UIView *containView = [UIView new]; containView.backgroundColor = [UIColor blackColor]; [scrollView addSubview:containView]; [containView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(scrollView); make.width.equalTo(scrollView); }]; UIView *lastView = nil; for (int i = 0; i < 6; i++) { UIView *sub_view = [UIView new]; sub_view.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 ) saturation:( arc4random() % 128 / 256.0 ) + 0.5 brightness:( arc4random() % 128 / 256.0 ) + 0.5 alpha:1]; [containView addSubview:sub_view]; [sub_view mas_makeConstraints:^(MASConstraintMaker *make) { make.left.and.right.equalTo(containView); make.height.mas_equalTo(@(30*i)); if (lastView) { make.top.mas_equalTo(lastView.mas_bottom); }else { make.top.mas_equalTo(containView.mas_top); } }]; lastView = sub_view; } // 最后更新containView [containView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(lastView.mas_bottom); }];}
5.【高阶】
。。。。。。。。
Demo github地址:Demo_Mansory
5 0
- 【iOS开发】Mansory自动布局(autolayout)使用教程
- iOS AutoLayout自动布局中级开发教程(1)-浅谈autolayout
- iOS AutoLayout自动布局中级开发教程(2)-初识autolayout
- iOS AutoLayout自动布局中级开发教程(2)-初识autolayout
- 65.iOS AutoLayout自动布局中级开发教程(2)-初识autolayout
- 66.iOS AutoLayout自动布局中级开发教程(1)-浅谈autolayout
- iOS开发技巧 autolayout自动布局
- iOS开发技巧之autolayout自动布局
- iOS开发之 AutoLayout自动布局
- iOS 开发之 AutoLayout 自动布局
- iOS开发技巧(autolayout自动布局)
- IOS开发-自动布局Autoresizing和Autolayout
- iOS AutoLayout自动布局中级开发教程(4)-label文字自动适应大小,宽度(新)
- iOS AutoLayout自动布局中级开发教程(4)-label文字自动适应大小,宽度(新)
- iOS AutoLayout自动布局中级开发教程(4)-label等文字自动适应大小,宽度
- iOS AutoLayout自动布局中级开发教程(4)-label等文字自动适应大小,宽度
- iOS Autolayout 自动布局代码使用规则
- ios Autolayout自动布局
- 【iOS开发】用instruments来检验你的app
- Linux 安装 rlwrap工具
- 【iOS开发】页面间跳转的性能优化
- 【读书】《禅与摩托车维修艺术》读后感
- 【Python开发】检测app使用了多少个xib和js的脚本(python)
- 【iOS开发】Mansory自动布局(autolayout)使用教程
- 备份还原工具NetBackUp7.6.1安装配置使用文档
- HDU 5354 Bipartite Graph 分治,并查集
- Binary Tree Level Order Traversal II
- linux多线程之自旋锁
- 百度地图SHA1值
- spring,mybatis事务管理配置与@Transactional注解使用
- Uva-10200 Prime Time 【素数+打表+浮点精度】
- 图论专题周赛回顾