Masonry小技巧总结


关于Masonry总是在看别人的,现在有时间了,也慢慢写点自己目前在项目中用到的一些比较实用的技巧。下载戳这里

**看这里**
**看这里**

一、ScrollView的contentSize

固定内容大小的就没啥好讲了!

下面我们讲下用masonry布局自动计算contentsize的竖向或横向大小。
原理:在scorllView添加子视图containerView,讲要添加的实际控件添加到containerView上,通过自动计算出来的containerView的大小来撑起scollView的内容,小了就不用谈了。

_scrollView = [[UIScrollView alloc] init];
_scrollView.backgroundColor= [UIColor whiteColor];
[_scrollViewmas_makeConstraints:^(MASConstraintMaker*make) {
   make.edges.equalTo(self.view);
  //******一定要有宽度**********
  make.width.equalTo(_scrollView);

}];
//ps:上面的约束我在网上看过,大多数都是只要第一句,我不知道他们是怎么实现的,反正我是没搞出来。
_containerView= [[UIView alloc] init];//需要添加的控件放到这个视图上
[_scrollView addSubview:_containerView];

竖向:

UIView*greenView = [UIViewnew];
greenView.backgroundColor= [UIColorgreenColor];
[_containerViewaddSubview:greenView];

[greenViewmas_makeConstraints:^(MASConstraintMaker*make) {
make.top.left.right.equalTo(_containerView);
make.height.mas_equalTo(2*screen_height);//screen_height为屏幕高度
}];

UIView*redView = [UIViewnew];
redView.backgroundColor= [UIColorredColor];
[_containerViewaddSubview:redView];

[redViewmas_makeConstraints:^(MASConstraintMaker*make) {
make.left.right.equalTo(_containerView);
make.top.equalTo(greenView.mas_bottom).offset(10);
make.height.mas_equalTo(screen_height);
//******一定要有**********
//这里相当于要对最后一个控件加五个约束条件,最后这个是用来让系统计算出containerView的大小的,以此撑起scrollVIew
make.bottom.equalTo(_containerView);
}];

横向:固定containerView的高度

make.height.equalTo(_scrollView),
//然后最后一个控件
make.bottom.equalTo(_containerView)

二、多控件相对父控件居中

- (void)viewDidLoad {
[super viewDidLoad];

[self.navigationItem setTitle:@"并列排序"];
[self setupFirstView];
[self setupSecondView];
}

- (void)setupFirstView{
_firstView = [UIView new];
_firstView.backgroundColor = kBlueColor;
[self.view addSubview:_firstView];
[_firstView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.top.equalTo(self.view);
    make.height.mas_equalTo(100);
}];

UIView *containerView = [UIView new];
[_firstView addSubview:containerView];

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
NSArray<UIButton *> *buttons = @[btn1,btn2,btn3];
[buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    obj.layer.cornerRadius = 5.f;
    [obj setTitle:@"btn" forState:0];
    obj.layer.borderColor = [UIColor whiteColor].CGColor;
    obj.layer.borderWidth = 1.f;
    [containerView addSubview:obj];
}];
//这个是水平排布子控件,MASAxisTypeHorizontal水平,FixedSpacing相对间距,leadSpacing头间距,tailSpacing尾间距
//还有个方法FixedItemLength,是固定子控件宽度或高度的
[buttons mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:0 tailSpacing:0];
[buttons mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(containerView);
    make.width.mas_equalTo(100);
    make.height.mas_equalTo(40);
}];
//有这个约束来控制整体的位置
[containerView mas_makeConstraints:^(MASConstraintMaker *make) {
    //宽度无需添加,子控件会把它撑开
    make.height.top.centerX.equalTo(_firstView);
}];
}

- (void)setupSecondView{
_secondView = [UIView new];
_secondView.backgroundColor = kBlueColor;
[self.view addSubview:_secondView];
[_secondView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.equalTo(self.view);
    make.top.equalTo(_firstView.mas_bottom).offset(20);
    make.height.mas_equalTo(100);
}];

UIView *containerView = [UIView viewWithBackgroundColor:kRedColor];
[_secondView addSubview:containerView];
UIView *leftView = [UIView viewWithBackgroundColor:[UIColor whiteColor]];
leftView.layer.cornerRadius = 10.f;
UIButton *centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[centerBtn setTitle:@"老牛座中央" forState:0];
centerBtn.backgroundColor = kGreenColor;
UILabel *rightLabel = [UILabel new];
rightLabel.text = @"互相伤害";
[containerView wj_addSuviews:@[leftView,centerBtn,rightLabel]];

[leftView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.centerY.equalTo(containerView);
    make.width.height.mas_equalTo(20);
}];
[centerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(leftView.mas_right);
    make.right.equalTo(rightLabel.mas_left);
    make.centerY.equalTo(containerView);
    make.width.mas_lessThanOrEqualTo(100);
}];
[rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(centerBtn.mas_right);
    make.centerY.right.equalTo(containerView);
    make.height.mas_equalTo(21);
    make.width.mas_lessThanOrEqualTo(100);
}];
//有这个约束来控制整体的位置
[containerView mas_makeConstraints:^(MASConstraintMaker *make) {
    //宽度无需添加,子控件会把它撑开
    make.center.equalTo(_secondView);
    make.height.mas_equalTo(50);
}];
}

三、UITableViewCell自动计算行高

tableView:

//这两行要有哦
//自动计算tableview的行高
self.tableView.rowHeight = UITableViewAutomaticDimension;
//估计高度需赋值,不然就是默认行高
self.tableView.estimatedRowHeight = 100;

cell:
- (void)initSubviews{
[self.contentView wj_addSuviews:@[self.avatar,self.name,self.imageV,self.details]];

[_avatar mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.left.equalTo(self.contentView).offset(10);
    make.size.mas_equalTo(CGSizeMake(50, 50));
}];

[_name mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(_avatar);
    make.left.equalTo(_avatar.mas_right).offset(20);
    make.right.equalTo(self.contentView).offset(-10);
    make.height.mas_equalTo(21);
}];

[_imageV mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.contentView).offset(10);
    make.right.equalTo(self.contentView).offset(-10);
    make.top.equalTo(_avatar.mas_bottom).offset(10);
    //priorityHigh()要有,不然会警告
    make.height.equalTo(_imageV.mas_width).priorityHigh();
}];
[_details mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.right.equalTo(_imageV);
    make.top.equalTo(_imageV.mas_bottom).offset(10);
    //高度不超过500,根据自身需求赋值就好
    make.height.mas_lessThanOrEqualTo(500);
    /*这个一定要有,也就是说底部控件需要五个约束条件
     *最后一个条件为相对contentView的约束
     */
    make.bottom.equalTo(self.contentView).offset(-10);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
    //这个一定要有,其实和test1中用containerView撑起来scollView的contentSize差不多
    //make.left.right.top.bottom.equalTo(self);
    //作用同上
    make.edges.equalTo(self).insets(UIEdgeInsetsZero);
}];
}

ps:这是目前能想到的,后期会持续更新,欢迎吐槽!!!

DEMO戳这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,252评论 6 516
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,886评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,814评论 0 361
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,869评论 1 299
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,888评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,475评论 1 312
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,010评论 3 422
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,924评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,469评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,552评论 3 342
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,680评论 1 353
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,362评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,037评论 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,519评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,621评论 1 274
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,099评论 3 378
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,691评论 2 361

推荐阅读更多精彩内容