self-sizing-in-UICollectionView
来源:互联网 发布:mcgs组态软件 编辑:程序博客网 时间:2024/05/20 22:27
给出最接近的预估大小
设置estimatedItemSize
,UICollectionView
需要拿这个值去设置一个初始的contentSize
,所以给一个最为接近的值,ios10中提供了一个UICollectionViewFlowLayoutAutomaticSize
的常量,这个值可以在cell
不断加载的过程中调整预估大小,使contentsize
的大小越来越接近实际大小。
设置好cell
的约束
与UITableView
不同的是,UICollectionView
在设置约束时,需要给定cell
横向的宽度,只有给定宽度值,才能让UICollectionViewCell
基于这个宽度得出高度值,不然两个值都是不确定的,不能使self-sizing正确发挥作用。
约束的设置大致如下:
UILabel * contentLabel = [UILabel new]; [self.contentView addSubview:contentLabel]; UILabel * titleLabel = [UILabel new]; titleLabel.numberOfLines = 0; titleLabel.font = [UIFont systemFontOfSize:20.0]; [self.contentView addSubview:titleLabel]; [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.contentView).offset(10); make.left.equalTo(self.contentView); make.right.equalTo(self.contentView).offset(-10); }]; self.titleLabel = titleLabel; self.contentView.backgroundColor = [UIColor redColor]; contentLabel.textColor = [UIColor blackColor]; contentLabel.font = [UIFont systemFontOfSize:15.0]; contentLabel.numberOfLines = 0; contentLabel.backgroundColor = [UIColor greenColor]; //ios8中可以给定宽度为CGRectGetWidth([UIScreen mainScreen].bounds) - 21 self.contentLabel = contentLabel; [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(titleLabel.mas_baseline).offset(10); make.left.equalTo(self.contentView).offset(10); make.right.equalTo(self.contentView).offset(-10); make.bottom.equalTo(self.contentView).offset(-10); make.width.equalTo(@(CGRectGetWidth([UIScreen mainScreen].bounds) - 20)); }];
iOS8
中出现的问题
在iOS8中发现一个bug,当cell
的width
与UICollectionView
的width
相同时,UICollectionView
就不能正常滚动了,应该是contentSize
的计算出了问题,为了解决这个问题,我把cell
的宽度较之UICollectionView
给小了一个像素,但这样并没有从根本上解决问题。今后的实践中还应继续探索,究竟什么原因导致的这个bug
一个小插曲
如果设置约束时使用了UILabel
的baseline
为参照,注意当label中的文字是中文,并且显示的文字是具有行间距的富文本时,这个参照是有问题的。
0 0
- self-sizing-in-UICollectionView
- self sizing table view cell
- estimatedRowHeight 与 self sizing cells
- Self Sizing Cells自适应的列表行
- Self Sizing Cells tableView 自适应高度
- self-sizing cell的一个问题
- 理解iOS 8中的Self Sizing Cells和Dynamic Type
- 理解iOS 8中的Self Sizing Cells和Dynamic Type
- swift实作app 交流分享八(self sizing cells)
- iOS8新功能——Self Sizing Cells和Dynamic Type
- iOS8 Self Sizing UITableView Cells iOS8Tableview Cells 自适应高度
- iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
- iOS8新特性Self Sizing Cells,使用estimatedRowHeight自适应cell
- Self Print in C++
- Assertion failure in -[UICollectionView layoutSublayersOfLayer:]
- iOS开发-UITableView在iOS11默认使用Self-Sizing造成contentSize计算问题的解决
- Self-Similar Processes in Telecommunications
- CentOS: RE-SIZING /dev/centos/root IN CENTOS7
- servlet3.1规范: 第10章 Web应用
- Linux网卡驱动程序详解
- [MSSQL]SQL Server 2008 收缩日志 清空删除大日志文件
- HTML+CSS基础 border-bottom在指定p标签的文本下方添加双细实线
- 源码-JavaScript&jQuery交互式前端开发-第3章-函数、方法与对象-浏览器对象模型
- self-sizing-in-UICollectionView
- 谷歌浏览器的DHC
- Java实现文件拷贝的4种方法(转)
- 归并排序(merging sort)
- String,StringBuffer与StringBuilder的区别??
- 【笔记】排列数与组合数(非完整)
- HTML+CSS基础 border-top在指定p标签的文本上方添加粗虚线
- MongoDB初识之CURD
- pid控制算法