将设备旋转为横向时的 UICollectionViewFlowLayout 大小警告 [英] UICollectionViewFlowLayout Size Warning When Rotating Device to Landscape

查看:25
本文介绍了将设备旋转为横向时的 UICollectionViewFlowLayout 大小警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 UICollectionView 来显示覆盖全屏的单元格(减去状态和导航栏).单元格大小从 self.collectionView.bounds.size 设置:

- (void)viewWillAppear:(BOOL)animated{////直到这里,顶栏的值不正确//CGSize tmpSize = self.collectionView.bounds.size;_currentCellSize = CGSizeMake( (tmpSize.width), (tmpSize.height));}- (CGSize)collectionView:(UICollectionView *)collectionView布局:(UICollectionViewLayout*)collectionViewLayoutsizeForItemAtIndexPath:(NSIndexPath *)indexPath {返回 _currentCellSize;}

这将为每个设备设置正确的大小.每个单元格都被定义为没有插图,并且布局没有页眉或页脚.但是,当我们从纵向旋转到横向时,我们会得到以下抱怨":

未定义 UICollectionViewFlowLayout 的行为,因为:项目高度必须小于 UICollectionView 的高度减去部分插入的顶部和底部值.

现在我理解了这个错误,但是我们重置了单元格的大小并使用了旋转过渡中内置的流布局:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration{//self.collectionView.bounds 仍然是最后一个大小...不是这里的新大小}- (void)didRotateFromInterfaceOrientation: UIInterfaceOrientation)fromInterfaceOrientation{CGSize tmpSize = self.collectionView.bounds.size;_currentCellSize = CGSizeMake( (tmpSize.width), (tmpSize.height));[self.collectionView performBatchUpdates:nil completion:nil];//这将强制重绘/调整单元格的大小.}

单元格在横向中正确渲染.

似乎流布局看到了旧的单元格大小(这会导致抱怨,因为它太高了),但确实读取/渲染了 didRotateFromInterfaceOrientation 中设置的新单元格大小.p>

有没有办法摆脱投诉?

我们尝试在设备旋转过渡期间找到另一个挂钩,该挂钩可以访问正确的目标屏幕尺寸(相对于当前屏幕尺寸),但没有成功.调试输出显示投诉发生在 willRotateToInterfaceOrientation 之后但在 didRotateFromInterfaceOrientation 之前.

我们也验证了显而易见的;如果我们将单元格高度设置为小于横向屏幕高度的固定大小,则不会发生投诉.此外,从横向旋转回纵向时不会发生投诉.

一切运行良好,并且渲染正确.然而,这种抱怨让我们感到担忧.其他人有什么想法或解决方案吗?

解决方案

我收到了同样的警告.对reloadData"方法不满意,我发现调用 [self.collectionView.collectionViewFlowLayout invalidateLayout] before 设置集合视图的框架可以消除警告并产生预期的结果.

We are using a UICollectionView to display cell that cover the full screen (minus the status and nav bar). The cell size is set from self.collectionView.bounds.size:

- (void)viewWillAppear:(BOOL)animated
{
    //
    // value isn't correct with the top bars until here
    //
    CGSize tmpSize = self.collectionView.bounds.size;
    _currentCellSize = CGSizeMake( (tmpSize.width), (tmpSize.height));
}

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

    return _currentCellSize;
}

This sets the correct sizing for each device. Each cell is defined to have no insets, and the layout has no header or footer. However, when we rotate from portrait to landscape we get the following "complaint":

the behavior of the UICollectionViewFlowLayout is not defined because:
the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

Now I understand this error, however we reset the size of the cell and use the flow layouts built in rotation transition:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //self.collectionView.bounds are still the last size...not the new size here
}
- (void)didRotateFromInterfaceOrientation: UIInterfaceOrientation)fromInterfaceOrientation
{
    CGSize tmpSize = self.collectionView.bounds.size;
    _currentCellSize = CGSizeMake( (tmpSize.width), (tmpSize.height));
    [self.collectionView performBatchUpdates:nil completion:nil];//this will force the redraw/size of the cells.
}

The cells render correctly in landscape.

It seems as though the Flow Layout sees the old cell size (which causes the complaint since it will be too tall), but does read/render the new cell size set in didRotateFromInterfaceOrientation.

Is there a way to get rid of the complaint?

We've tried finding another hook during a device rotate transition that has access to the correct target screen size (vs the current screen size) with no luck. Debug output shows the complaint happens after willRotateToInterfaceOrientation but before didRotateFromInterfaceOrientation.

We've also verified the obvious; if we set up the cell height to be a fixed size less than the landscape screen height, the complaint doesn't occur. Also, the complaint does not occur when rotating from landscape back to portrait.

Everything runs fine, and renders correctly. However this complaint worries us. Anyone else have any ideas or solutions?

解决方案

I was getting the same warning. Unsatisfied with the "reloadData" approach, I found that calling [self.collectionView.collectionViewFlowLayout invalidateLayout] before setting the frame of the collection view silenced the warning and yielded the expected results.

这篇关于将设备旋转为横向时的 UICollectionViewFlowLayout 大小警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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