关于 collectionView的layout布局方法:

设置cell的间距,行间距,组与组之间的间距,都是在layout里面来设置.

包括,滚动方向.

-(void)prepareLayout

[super prepareLayout]

//最小行间距

self.minimumLineSpacing =1;

//最小cell间距

self.minimumInteritemSpacing =1;

//组与组之间的间距

self.sectionInset =UIEdgeInsetsMake(0, 0, 20, 0);

}

设置cell的大小有两种方法

一种在layout布局里面来通过方法,设置cell的大小

方法名称比较长,只要记住layout的关键字返回值是一个数组

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

NSArray *attrs =[super layoutAttributesForElementsInRect:rect];

for (UICollectionViewLayoutAttributes *attr in attrs) {

//如果是第一组,进行操作..

//获取frame

CGRect frame = attr.frame;

CGFloat height =100;

CGFloat width =(self.collectionView.frame.size.width -3)/4;

if (attr.indexPath.section ==1) {

//frame = CGRectMake(0, 0, self.collectionView.frame.size.width, 100);//不要修改他的位置,只需要修改他的大小

//修改frame

frame.size =CGSizeMake(self.collectionView.frame.size.width, height);

}else{

frame.size =CGSizeMake(width, height);

}

//赋值回去

attr.frame =frame;

}

return attrs;

}

第二种方法:

遵守协议

//实现代理方法

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

CGFloat height = 100;

CGFloat width =(collectionView.frame.size.width-3)/4;

if (indexPath.section ==1) {

return CGSizeMake(collectionView.frame.size.width, 120);

}

return CGSizeMake(width, height);

}

-(void)prepareLayout{

[super prepareLayout];

//最小行间距

self.minimumLineSpacing =1;

//最小cell间距

self.minimumInteritemSpacing =1;

//组与组之间的间距

self.sectionInset =UIEdgeInsetsMake(0, 0, 20, 0);

CGFloat height =100;

CGFloat width =(self.collectionView.frame.size.width -3)/4;

self.itemSize =CGSizeMake(width, height);

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//上面的布局问题:需要初始时候先指定一个大小. 当初始位置有了大小,让布局,按照这个大小来进行布局

//没有给大小,他会按照默认的大小,50,50来计算,所以会导致cell直接压在一起.

-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

NSArray *attrs =[super layoutAttributesForElementsInRect:rect];

for (UICollectionViewLayoutAttributes *attr in attrs) {

//如果是第一组,进行操作..

//获取frame

CGRect frame = attr.frame;

if (attr.indexPath.section ==1) {

frame.size =CGSizeMake(self.collectionView.frame.size.width, 100);

}

//赋值回去

attr.frame =frame;

}

return attrs;

}

@end

最新文章

  1. Delphi的三目运算 ifthen 和iif
  2. bzoj1491 社交网络
  3. C#创建https请求并使用pfx证书
  4. [置顶] 白话二分匹配之最大匹配+附上hdu2063解题报告
  5. iOS基础 - UIDatePicker and UIPickerView and UITextField
  6. 模板不存在:./xx 错误位置 FILE: LINE:110 (thinkphp上传至服务器后模板无法解析原因)
  7. JAVA_SE基础——71.Random类制作随机验证码
  8. poj1182-食物链-带权并查集-种类并查集
  9. [android] 隐式意图和显式意图的使用场景
  10. Quartz简答介绍
  11. SQLserver 还原数据库报“指定转换无效”的错的解决方案
  12. 使用css将图像居中
  13. f5 SNMP配置
  14. 11_python_闭包迭代器
  15. 【树莓派】树莓派Android相关资料
  16. 【Servlet】关于RequestDispatcher的原理
  17. 如何用 纯C++(ndk)开发安卓应用 ?
  18. logback日志模板与详解
  19. 使用什么工具连接MySQL Server
  20. Yii2 模块module笔记

热门文章

  1. AC日记——统计难题 hdu 1251
  2. 关于js中for in的缺陷浅析
  3. 理解 Objective-C Runtime
  4. Bagging决策树:Random Forests
  5. bzoj1061--线性规划
  6. setTimeout小总结
  7. DBCC CHECKIDENT在 SQL Server修改指定表的当前标识值
  8. SQL SERVER运维日记--收缩数据库
  9. 读书笔记 effective c++ Item4 确保对象被使用前进行初始化
  10. webapi 发布接口报405错误(angularjs2.0)