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

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

问题描述

2015-08-18 16:07:51.523 Example[16070:269647] 的行为UICollectionViewFlowLayout 未定义,因为:2015-08-1816:07:51.523
Example[16070:269647] 项目宽度必须小于UICollectionView 的宽度减去左侧的部分插入和右值,减去内容插入左值和右值.
2015-08-18 16:07:51.524 示例[16070:269647] 相关UICollectionViewFlowLayout 实例是 <UICollectionViewFlowLayout:0x7f24d670>,并附加到<UICollectionView: 0x7b377200;帧 = (0 0; 768 1024);clipsToBounds = YES;自动调整大小 = W+H;手势识别器 = <NSArray: 0x7f24ce30>;动画 = {位置=<CABasicAnimation: 0x7f279d60>;bounds.origin=<CABasicAnimation: 0x7f279430>;bounds.size=<CABasicAnimation: 0x7f279480>;};层 = <CALayer:0x7aec9cf0>;内容偏移:{0, 0};内容大小:{1024, 770}>集合视图布局:<UICollectionViewFlowLayout:0x7f24d670>.2015-08-18 16:07:51.524 示例[16070:269647] 做一个象征性的要捕获的 UICollectionViewFlowLayoutBreakForInvalidSizes 处的断点这在调试器中.

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 <UICollectionViewFlowLayout: 0x7f24d670>, and it is attached to <UICollectionView: 0x7b377200; frame = (0 0; 768 1024); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x7f24ce30>; animations = { position=<CABasicAnimation: 0x7f279d60>; bounds.origin=<CABasicAnimation: 0x7f279430>; bounds.size=<CABasicAnimation: 0x7f279480>; }; layer = <CALayer: 0x7aec9cf0>; contentOffset: {0, 0}; contentSize: {1024, 770}> collection view layout: <UICollectionViewFlowLayout: 0x7f24d670>. 2015-08-18 16:07:51.524 Example[16070:269647] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

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

That is what I get and what I do

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

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

I rotate from landscape to portrait then the console shows this error message only in iOS 9 if anyone knows what 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)

更新以包含 Swift 4

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.

发生的情况是您的集合视图流布局子类没有覆盖 shouldInvalidateLayoutForBoundsChange 函数,默认情况下 返回 false.

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

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

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.

这意味着两件事:

1 - 警告本身无害

2 - 您可以通过简单地覆盖 shouldInvalidateLayoutForBoundsChange 函数以返回 true 来摆脱它.在这种情况下,当集合视图边界发生变化时,将始终调用流布局.

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天全站免登陆