iphone之利用Masonry框架为组件创建约束
来源:互联网 发布:搭建阿里云服务器 编辑:程序博客网 时间:2024/06/10 00:56
Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性,而且同时支持 iOS 和 Max OS X。Masonry是一个用代码写iOS或OS界面的库,可以代替Auto layout。Masonry的github地址:https://github.com/SnapKit/Masonry
本章内容
- Masonry配置
- Masonry使用
- Masonry实例
Masonry配置
- 推荐使用pods方式引入类库,pod 'Masonry',若不知道pod如何使用,情况我的另一篇文章: 提高ios开发效率的工具
- 引入头文件 #import "Masonry.h"
Masonry使用讲解
mas_makeConstraints 是给view添加约束,约束有几种,分别是边距,宽,高,左上右下距离,基准线。添加过约束后可以有修正,修正有offset(位移)修正和multipliedBy(倍率)修正。
语法一般是 make.equalTo or make.greaterThanOrEqualTo or make.lessThanOrEqualTo + 倍数和位移修正。
注意点1: 使用 mas_makeConstraints方法的元素必须事先添加到父元素的中,例如[self.view addSubview:view];
注意点2: masequalTo 和 equalTo 区别:masequalTo 比equalTo多了类型转换操作,一般来说,大多数时候两个方法都是 通用的,但是对于数值元素使用mas_equalTo。对于对象或是多个属性的处理,使用equalTo。特别是多个属性时,必须使用equalTo,例如 make.left.and.right.equalTo(self.view);
注意点3: 注意到方法with和and,这连个方法其实没有做任何操作,方法只是返回对象本身,这这个方法的左右完全是为了方法写的时候的可读性 。make.left.and.right.equalTo(self.view);和make.left.right.equalTo(self.view);是完全一样的,但是明显的加了and方法的语句可读性 更好点。
导入Masonry框架
并把头文件 #import"Masonry.h"放到pch文件中方便整个工程使用
具体代码如下:
//// ViewController.m// Masonry使用介绍(一)//// Created by apple on 15/9/17.// Copyright (c) 2015年 LiuXun. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // [self exp1]; // [self exp2]; [self exp3];}-(void)exp1{ // exp1: 中心点与self.view相同 宽度为400*400 UIView *view = [UIView new]; [view setBackgroundColor:[UIColor redColor]]; [self.view addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.view); // 中心点的的坐标与self.view的相同 make.size.mas_equalTo(CGSizeMake(300, 150)); // 等于具体数值时用mas_equalTo }];}-(void)exp2{ // 上下左右边距都为10 UIView *view = [UIView new]; [view setBackgroundColor:[UIColor yellowColor]]; [self.view addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) {// make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(30, 30, 30, 30)); // inset表示内边距 make.left.equalTo(self.view).with.offset(30); make.right.equalTo(self.view).with.offset(-30); make.top.equalTo(self.view).with.offset(30); make.bottom.equalTo(self.view).with.offset(-30); }];}-(void)exp3{ //让两个高度为150的View垂直居中且等宽等间隔排列 间隔为10 UIView *view1 = [UIView new]; [self.view addSubview:view1]; [view1 setBackgroundColor:[UIColor redColor]]; UIView *view2 = [UIView new]; [view2 setBackgroundColor:[UIColor blueColor]]; [self.view addSubview:view2]; [view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.view.mas_centerY); make.height.mas_equalTo (150); make.width.mas_equalTo(view2.mas_width); make.left.mas_equalTo(self.view.mas_left).with.offset(10); make.right.mas_equalTo(view2.mas_left).with.offset(-10); }]; [view2 mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.view.mas_centerY); make.height.mas_equalTo(150); make.right.mas_equalTo(self.view.mas_right).with.offset(-10); make.width.mas_equalTo(view1.mas_width); }];} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end运行结果如下:
运行第二个方法如下
运行第三个方法如:
- iphone之利用Masonry框架为组件创建约束
- 利用Masonry进行 约束判断
- iphone第十节:屏幕适配(Masonry约束)
- Masonry学习之更新约束
- Masonry约束
- iOS学习之Masonry第三方约束
- Masonry学习之设置新的约束
- 利用masonry添加约束的控件, 添加部分圆角
- MySQL约束之创建约束
- iOS 自动布局框架学习之Masonry
- iOS自动布局框架之Masonry
- iOS开发之Masonry框架源码解析
- Masonry — 代码约束
- masonry 约束布局
- Masonry约束的使用
- masonry延时约束bug
- Masonry框架
- iPhone开发之UIScrollView滚动组件的使用(七)利用NSTimer计时器和UIPageControl组件代码实现图片轮播器
- Sorting a Three-Valued Sequence(USACO 2.1.3)
- MapReduce(四):shuffer原理
- ubuntu14.04环境下python3 Image.show()不显示图片问题
- capistrano2和3生成Capfile语句的区别
- 没有用户画像,别谈精准营销
- iphone之利用Masonry框架为组件创建约束
- 大数据量时Mysql的优化要点[转]
- Android ListView的addHeaderView与addFooterView的详解
- 利用ViewPager、PagerTabStrip实现滚动导航
- HDOJ1010.Tempter of the Bone
- PHP strtr与str_replace函数性能简单测试
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Te
- 动态访问某个方法
- android 开发 小技巧