Masonry — 代码约束

来源:互联网 发布:淘宝vip 编辑:程序博客网 时间:2024/05/16 03:31

Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X。

Masonry是一个非常优秀的autolayout库 能够节省大量的开发和学习时间

Masonry支持的一些属性

// 左侧@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;// 中心点x@property (nonatomic, strong, readonly) MASConstraint *centerX;// 中心点y@property (nonatomic, strong, readonly) MASConstraint *centerY;// 文本基线@property (nonatomic, strong, readonly) MASConstraint *baseline;

这些属性与NSLayoutAttrubute的对照表如下

下面是一个Masory工程的Demo
这里写图片描述

////  ViewController.m//  Masnory_Demo////  Created by lanou3g on 15/10/29.//  Copyright © 2015年 . All rights reserved.//#import "ViewController.h"#import "Masonry.h"#import "UIView+masnory.h"#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    WS(ws);    //从此以后基本可以抛弃CGRectMake了    UIView *sv = [UIView new];    sv.backgroundColor = [UIColor blackColor];    //在做autoLayout之前 一定要先将view添加到superview上 否则会报错    [self.view addSubview:sv];    //mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了    [sv mas_makeConstraints:^(MASConstraintMaker *make) {        //将sv居中        make.center.equalTo(ws.view);         //将size设置成(300,300)        make.size.mas_equalTo(CGSizeMake(300, 300));    }];
#pragma mark --  [初级] 让一个view略小于其superView(边距为10)
//    //    UIView *sv1 = [UIView new];//    sv1.backgroundColor = [UIColor redColor];//    [sv addSubview:sv1];//    //距离sv这个视图的边距//    [sv1 mas_makeConstraints:^(MASConstraintMaker *make) {//        make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));//        //        /* 等价于//         make.top.equalTo(sv).with.offset(10);//         make.left.equalTo(sv).with.offset(10);//         make.bottom.equalTo(sv).with.offset(-10);//         make.right.equalTo(sv).with.offset(-10);//         *///        //        /* 也等价于//         make.top.left.bottom.and.right.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));//         *///    }];
#pragma mark --  [初级]  让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10(自动计算其宽度)
//    UIView *sv2 = [UIView new];//    UIView *sv3 = [UIView new];//    sv2.backgroundColor = [UIColor blueColor];//    sv3.backgroundColor = [UIColor yellowColor];//    //    [sv1 addSubview:sv2];//    [sv1 addSubview:sv3];//    //    int padding1 = 10;//    [sv2 mas_makeConstraints:^(MASConstraintMaker *make) {//        make.centerY.mas_equalTo(sv1.mas_centerY);//        make.left.equalTo(sv1.mas_left).with.offset(padding1);//        make.right.equalTo(sv3.mas_left).with.offset(-padding1);//        make.height.mas_equalTo(@150);//        make.width.equalTo(sv3);//    }];//  //    [sv3 mas_makeConstraints:^(MASConstraintMaker *make) {//        make.centerY.mas_equalTo(sv1.mas_centerY);//        make.left.equalTo(sv2.mas_right).with.offset(padding1);//        make.right.equalTo(sv1.mas_right).with.offset(-padding1);//        make.height.mas_equalTo(@150);//     //   make.width.equalTo(sv2);  上面已经写过与其相等//    }];#pragma mark -- [中级] 在UIScrollView顺序排列一些view并自动计算contentSize    UIScrollView *scrollView = [UIScrollView new];    scrollView.backgroundColor = [UIColor whiteColor];    [sv addSubview:scrollView];    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(5,5,5,5));    }];    UIView *container = [UIView new];    [scrollView addSubview:container];    [container mas_makeConstraints:^(MASConstraintMaker *make) {        make.edges.equalTo(scrollView);        make.width.equalTo(scrollView);    }];    int count = 10;    UIView *lastView = nil;    for ( int i = 1 ; i <= count ; ++i )    {        UIView *subv = [UIView new];        [container addSubview:subv];        subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )                                          saturation:( arc4random() % 128 / 256.0 ) + 0.5                                          brightness:( arc4random() % 128 / 256.0 ) + 0.5                                               alpha:1];        [subv mas_makeConstraints:^(MASConstraintMaker *make) {            make.left.and.right.equalTo(container);            make.height.mas_equalTo(@(20*i));            if ( lastView )            {                make.top.mas_equalTo(lastView.mas_bottom);            }            else            {                make.top.mas_equalTo(container.mas_top);            }        }];        lastView = subv;    }    [container mas_makeConstraints:^(MASConstraintMaker *make) {        make.bottom.equalTo(lastView.mas_bottom);    }];
#pragma mark --  [高级] 横向或者纵向等间隙的排列一组view
    UIView *sv11 = [UIView new];    UIView *sv12 = [UIView new];    UIView *sv13 = [UIView new];    UIView *sv21 = [UIView new];    UIView *sv31 = [UIView new];    sv11.backgroundColor = [UIColor blackColor];    sv12.backgroundColor = [UIColor blackColor];    sv13.backgroundColor = [UIColor blackColor];    sv21.backgroundColor = [UIColor blackColor];    sv31.backgroundColor = [UIColor blackColor];    [sv addSubview:sv11];    [sv addSubview:sv12];    [sv addSubview:sv13];    [sv addSubview:sv21];    [sv addSubview:sv31];    //给予不同的大小 测试效果    [sv11 mas_makeConstraints:^(MASConstraintMaker *make) {        make.centerY.equalTo(@[sv12,sv13]);        make.centerX.equalTo(@[sv21,sv31]);        make.size.mas_equalTo(CGSizeMake(40, 40));    }];    [sv12 mas_makeConstraints:^(MASConstraintMaker *make) {        make.size.mas_equalTo(CGSizeMake(70, 20));    }];    [sv13 mas_makeConstraints:^(MASConstraintMaker *make) {        make.size.mas_equalTo(CGSizeMake(50, 50));    }];    [sv21 mas_makeConstraints:^(MASConstraintMaker *make) {        make.size.mas_equalTo(CGSizeMake(50, 20));    }];    [sv31 mas_makeConstraints:^(MASConstraintMaker *make) {        make.size.mas_equalTo(CGSizeMake(40, 60));    }];    [sv distributeSpacingHorizontallyWith:@[sv11,sv12,sv13]];    [sv distributeSpacingVerticallyWith:@[sv11,sv21,sv31]];}@end

使用方法http://www.cocoachina.com/ios/20141219/10702.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 冒险岛法师运气不够怎么办 cad文件打开老是闪退白屏怎么办 上古卷轴5坏档怎么办 冒险岛2 65级后怎么办 第二次起诉离婚对方不同意怎么办 起诉离婚对方不同意离婚怎么办 我12爸妈离婚该怎么办 苹果迅雷下载的链接打不开怎么办 苹果上装的迅雷打不开怎么办 mp4进水水干了后打不开怎么办 迅雷闪退ios10.3怎么办 用辣椒辣到眼睛怎么办 辣椒辣到眼皮了怎么办 眼被辣椒辣了怎么办 老公欠银行的钱怎么办 离婚老公欠的钱怎么办 欠了几十万债怎么办 执行死刑时已怀孕怎么办 执行死刑世已经怀孕怎么办 孩子被爸爸打该怎么办 二年级的孩子逃课怎么办 龙妈和雪诺怎么办 车本扣6分怎么办 超速50扣了12分怎么办 开车超速扣12分怎么办 超速一次记12分怎么办? 驾照超速扣12分怎么办 c牌一次扣12分怎么办 超速罚500扣12分怎么办 高速超速记3分怎么办 高速超速记12分怎么办 过江隧道关闭了怎么办 高速限速60超了怎么办 超速扣了12分怎么办 上海 12分扣完了怎么办 狗抓伤只淌白沫怎么办 刑事案件法院迟迟不判决怎么办 法院判决书下来了没钱赔偿怎么办 被起诉开庭不去怎么办 地道塌陷造成房屋倒塌怎么办 海外看不了b站怎么办