前言

说到iOS自动布局,有很多的解决办法。有的人使用xib/storyboard自动布局,也有人使用frame来适配。对于前者,笔者并不喜欢,也不支持。对于后者,更是麻烦,到处计算高度、宽度等,千万大量代码的冗余,对维护和开发的效率都很低。

笔者在这里介绍纯代码自动布局的第三方库:Masonry。这个库使用率相当高,在全世界都有大量的开发者在使用,其star数量也是相当高的。

效果图

本节详解Masonry的以动画的形式更新约束的基本用法,先看看效果图:

我们这里初始按钮是一个很小的按钮,点击就不断放大,最大就放大到全屏幕。

核心代码

看下代码:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
#import "TableViewController.h"
#import "TestCell.h"
 
@interface TableViewController () <UITableViewDataSource, UITableViewDelegate>
 
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
 
@end
 
@implementation TableViewController
 
- (void)viewDidLoad {
  [super viewDidLoad];
  
  self.tableView = [[UITableView alloc] init];
  self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  self.tableView.delegate = self;
  self.tableView.dataSource = self;
  [self.view addSubview:self.tableView];
  [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(self.view);
  }];
  
  for (NSUInteger i = 0; i < 10; ++i) {
    TestModel *model = [[TestModel alloc] init];
    model.title = @"测试标题,可能很长很长,反正随便写着先吧!";
    model.desc = @"描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,描述内容通常都是很长很长的,";
    
    [self.dataSource addObject:model];
  }
  
  [self.tableView reloadData];
}
 
- (NSMutableArray *)dataSource {
  if (_dataSource == nil) {
    _dataSource = [[NSMutableArray alloc] init];
  }
  
  return _dataSource;
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.dataSource.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *cellIdentifier = @"CellIdentifier";
 
  TestCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  
  if (!cell) {
    cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }
  
  cell.indexPath = indexPath;
  cell.block = ^(NSIndexPath *path) {
    [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
  };
  TestModel *model = [self.dataSource objectAtIndex:indexPath.row];
  [cell configCellWithModel:model];
  
  return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  TestModel *model = [self.dataSource objectAtIndex:indexPath.row];
  
  return [TestCell heightWithModel:model];
}
 
 
@end
 

讲解


我们来看看这个计算行高的代码,看起来是不是很像配置数据的代理方法呢?

 
1
2
3
4
5
6
7
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  TestModel *model = [self.dataSource objectAtIndex:indexPath.row];
  
  return [TestCell heightWithModel:model];
}
 

我们看看TestCell的声明,提供了一个计算行高的类方法:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
 
typedef void (^TestBlock)(NSIndexPath *indexPath);
 
@interface TestCell : UITableViewCell
 
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) NSIndexPath *indexPath;
 
@property (nonatomic, copy) TestBlock block;
 
- (void)configCellWithModel:(TestModel *)model;
 
+ (CGFloat)heightWithModel:(TestModel *)model;
 
@end
 

我们看一下计算行高的实现:

 
1
2
3
4
5
6
7
8
9
10
11
 
+ (CGFloat)heightWithModel:(TestModel *)model {
  TestCell *cell = [[TestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
  [cell configCellWithModel:model];
  
  [cell layoutIfNeeded];
  
  CGRect frame =  cell.descLabel.frame;
  return frame.origin.y + frame.size.height + 20;
}
 

我们只是创建了一个cell然后配置数据,然后调用layoutIfNeeded更新约束,以便获取到frame。当我们获取到以后,我们就可以计算出最后的cell真正的高度了。

最新文章

  1. ASP.Net MVC——DotNetZip简单使用,解决文件压缩问题。
  2. VS2015 编译 Qwt6.1.3
  3. HDOJ 4336 Card Collector
  4. Porter/Duff,图片加遮罩setColorFilter
  5. exjs3.2的gridPanel的表头总宽度与列的总宽度不一致的解决方案
  6. orzdba的安装与使用
  7. Microsoft Press Free eBook
  8. Spring整合CXF,发布RSETful 风格WebService(转)
  9. Android开发 |常见的内存泄漏问题及解决办法
  10. c# 单例模式[Singleton]之深夜闲聊
  11. 连接池 BoneCPDataSource
  12. 你好,C++(2)1.3 C++世界版图1.4 如何学好C++
  13. 异步的 SQL 数据库封装
  14. 线程、线程句柄、线程ID
  15. 算法导论——lec 10 图的基本算法及应用
  16. 数据库MySQL调优实战经验总结
  17. centos7下安装docker(18docker日志---docker logs)
  18. 解压文件出错解决方法(invalid compressed data--format violated)
  19. 保存数据到Excel中
  20. SpringMVC接受JSON参数详解及常见错误总结我改

热门文章

  1. BZOJ4551 - [TJOI2016]树
  2. JavaBean映射工具dozer学习
  3. 【ZJOI2017 Round1练习】D2T3 counter(线段树)
  4. js82:CSS的Style,image的重定位,getElementById,getElementsByTagName,location.href
  5. SQL SERVER 2012 第三章 T-SQL 基本语句 having子句
  6. POJ 1144 割点
  7. Delphi控件大全
  8. Segments--poj3304(判断直线与线段之间的关系)
  9. SQL根据某一父节点查询所有子节点,无限
  10. 解决Win7 64bit + VS2013 使用opencv时出现提“应用程序无法正常启动(0xc000007b)”错误