未定义UICollectionViewFlowLayout的行为,因为单元格宽度大于collectionView宽度 [英] the behavior of the UICollectionViewFlowLayout is not defined, because the cell width is greater than collectionView width

查看:1392
本文介绍了未定义UICollectionViewFlowLayout的行为,因为单元格宽度大于collectionView宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2015-08-18 16:07:51.523示例[16070:269647]的行为 未定义UICollectionViewFlowLayout的原因是:2015-08-18 16:07:51.523
Example [16070:269647]项目宽度必须小于 UICollectionView的宽度减去该部分的左插图, 右值减去内容插入左和右值.
2015-08-18 16:07:51.524示例[16070:269647]相关 UICollectionViewFlowLayout实例为,并且已附加到;动画= { position =; bounds.origin =; bounds.size =; };层=; contentOffset:{0,0}; contentSize:{1024,770}> 集合视图布局:. 2015-08-18 16:07:51.524例子[16070:269647]做一个象征 UICollectionViewFlowLayoutBreakForInvalidSizes处的断点以捕获 在调试器中.

这就是我得到的,我所做的就是

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(self.collectionView!.frame.size.width - 20, 66)
    }

当我从横向旋转到纵向时,控制台仅在iOS 9中显示此错误消息,是否有人知道会发生什么,并且是否有此问题的解决方法?

解决方案

当您的收藏夹视图调整为较小的宽度(例如,从横向模式变为纵向模式)并且该单元格变得太大而无法容纳时,会发生这种情况./p>

为什么单元格会变得太大,因为应该调用集合视图流布局并返回合适的大小?

collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)

更新为包含Swift 4

@objc override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{  ...  }

这是因为调用了此函数,或者至少不是立即调用了此函数.

发生的事情是,您的集合视图流布局子类未覆盖shouldInvalidateLayoutForBoundsChange函数,默认情况下为returns false.

此方法返回false时,收集视图首先尝试使用当前单元格的大小,检测到问题(记录警告),然后调用流布局以调整单元格的大小.

这意味着两件事:

1-警告本身无害

2-您可以通过简单地重写shouldInvalidateLayoutForBoundsChange函数以返回true来摆脱它. 在这种情况下,当集合视图范围更改时,流布局将始终被调用.

2015-08-18 16:07:51.523 Example[16070:269647] the behavior of the UICollectionViewFlowLayout is not defined because: 2015-08-18 16:07:51.523
Example[16070:269647] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2015-08-18 16:07:51.524 Example[16070:269647] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; animations = { position=; bounds.origin=; bounds.size=; }; layer = ; contentOffset: {0, 0}; contentSize: {1024, 770}> collection view layout: . 2015-08-18 16:07:51.524 Example[16070:269647] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

This is what I get, what I do is

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(self.collectionView!.frame.size.width - 20, 66)
    }

when I rotate from landscape to portrait, the console shows this error message only in iOS 9, does anyone know what this happens and if there is a fix for this?

解决方案

This happens when your collection view resizes to something less wide (go from landscape to portrait mode, for example), and the cell becomes too large to fit.

Why is the cell becoming too large, as the collection view flow layout should be called and return a suitable size ?

collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)

Update to include Swift 4

@objc override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{  ...  }

This is because this function is not called, or at least not straight away.

What happens is that your collection view flow layout subclass does not override the shouldInvalidateLayoutForBoundsChange function, which returns false by default.

When this method returns false, the collection view first tries to go with the current cell size, detects a problem (which logs the warning) and then calls the flow layout to resize the cell.

This means 2 things :

1 - The warning in itself is not harmful

2 - You can get rid of it by simply overriding the shouldInvalidateLayoutForBoundsChange function to return true. In that case, the flow layout will always be called when the collection view bounds change.

这篇关于未定义UICollectionViewFlowLayout的行为,因为单元格宽度大于collectionView宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆