将设备旋转到横向时,UICollectionViewFlowLayout大小警告
我们正在使用UICollectionView来显示覆盖全屏的单元格(减去状态和导航栏).像元大小设置为self.collectionView.bounds.size
:
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.
流程布局"似乎看到了旧的单元格大小(这会引起投诉,因为它太高了),但确实读取/渲染了在didRotateFromInterfaceOrientation
中设置的新的单元格大小.
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?
我们尝试在设备旋转过渡期间找到另一个钩子,该钩子可以访问正确的目标屏幕尺寸(相对于当前屏幕尺寸),但没有运气.调试输出显示投诉发生在willRotateToInterfaceOrientation
之后但在didRotateFromInterfaceOrientation
之前.
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?
我收到了同样的警告.对于"reloadData"方法不满意,我发现调用[self.collectionView.collectionViewFlowLayout invalidateLayout]
之前设置收集视图的框架会使警告静音,并产生了预期的结果.
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.