美文网首页
Masonry小技巧总结

Masonry小技巧总结

作者: _Boring | 来源:发表于2017-07-28 13:24 被阅读46次

关于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戳这里

相关文章

  • Masonry小技巧总结

    一、ScrollView的contentSize 固定内容大小的就没啥好讲了! 下面我们讲下用masonry布局自...

  • Masonry 单个约束的移除(卸载)和安装

    Masonry小技巧门来自 https://github.com/SnapKit/Masonry/issues/1...

  • Masonry小技巧

    当你写了一个错误的constanints,例如: 然后在你会看到如下的debug log: 是不是会眼花,如果有很...

  • iOS小技巧总结

    iOS小技巧总结### 参考iOS_小松哥的iOS小技巧总结 总结日常代码生活中的小技巧,随时可用 阿拉伯数字转中...

  • iOS小技巧总结,绝对有你想要的

    iOS小技巧总结,绝对有你想要的 iOS小技巧总结,绝对有你想要的

  • Autolayout、VFL、Masonry

    适配 VFL语言 Masonry 代码实现Autolayout VFL代码 Masonry使用 总结 使用代码实现...

  • 实用iOS第三方框架

    界面布局 github地址:Masonry Masonry使用总结 : 赵不懂的博客 网络请求 github地址...

  • Masonry使用总结

    Masonry使用总结 一、Masonry简介 Masonry是一个轻量级的布局框架,适用于iOS以及OS X。它...

  • Masonry自适应UISrollview

    Masonry自适应UISrollview 总结:1.显示视图使用Masonry对srollview作约束,使to...

  • Masonry总结

    学习Masonry最好的方式就是下载官方的Demo,简单的布局我就不说了,直接看https://github.co...

网友评论

      本文标题:Masonry小技巧总结

      本文链接:https://www.haomeiwen.com/subject/qxmokxtx.html