UICollectionView the item height must be less than the height of the UICollectionView minus the sect

在写Demo的时候发现了打印台有项目里类似的警告,想着正好有时间,可以看看怎么去除这个警告。

警告如下:

the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

UICollectionView the item height must be less than the height of the UICollectionView minus the sect_第1张图片

看着意思大概是说item的大小超出了collectionView的大小 。

但是看着代码逻辑上应该没错啊,大小正好限制的好好的。然后debug了一下,看了一下图层关系,看到了点蛛丝马迹

UICollectionView the item height must be less than the height of the UICollectionView minus the sect_第2张图片 

UICollectionView the item height must be less than the height of the UICollectionView minus the sect_第3张图片 

我理想中的设置collectionView的Height是14.32这样的高度,然后设置的label的Height就是充盈这个cell。感觉理论上没啥问题,但是日志台上有这么一长串的警告输出。查找资料之后,受到启发,看了下层级关系:

UICollectionView the item height must be less than the height of the UICollectionView minus the sect_第4张图片

collectionView的实际高度是14,而cell的实际高度是14.333

 UICollectionView the item height must be less than the height of the UICollectionView minus the sect_第5张图片

所以有这个报错就很正常了。但是我有点没有搞懂,为啥masonary传入带小数的数之后,是以什么规则计算得到最终的值的。因为我试了好几种数值带小数的,并不是我想的,传入小数,可能就向下取整了。传入不同的数值,从图层看到的Height还不太一样,基本上不是预期的。但是传入整数,得到的就是预期的整数。所以目前我的处理方式就是向上取整,来解决这个警告。就是保证collectionView的高度比cell的大,或者相等。

CGFloat height_tags = ceil([@"先婚后爱" sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}].height);
[self.collecitonView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.centerY.equalTo(@0);
     make.left.equalTo(@44);
     make.right.equalTo(@-44);
     make.height.equalTo(@(height_tags));
 }];

 

你可能感兴趣的:(ios)