gpt4 book ai didi

ios - UICollectionViewFlowLayout 不会使方向更改的权利失效

转载 作者:IT王子 更新时间:2023-10-29 08:00:40 25 4
gpt4 key购买 nike

我有一个带有 UICollectionViewFlowLayout 的 UICollectionView。我还实现了 UICollectionViewDelegateFlowLayout 协议(protocol)。

在我的数据源中,我有一堆响应自定义协议(protocol)的 UIViewController,因此我可以询问它们的大小和其他一些东西。

在 FlowLayout 委托(delegate)中,当它要求 sizeForItemAtIndexPath: 时,我返回从我的协议(protocol)中获得的项目大小。实现我的协议(protocol)的 ViewControllers 根据方向返回不同的项目大小。

现在,如果我将设备方向从纵向更改为横向,没有问题(横向的项目更大)但如果我将其更改回去,我会收到此警告:

    the item width must be less that the width of the UICollectionView minus the section insets left and right values.
Please check the values return by the delegate.

它仍然有效,但我不喜欢它收到警告,所以也许你可以告诉我我做错了什么。还有另一个问题。如果我不告诉我的 collectionViews collectionViewLayout 在 willAnimateRotationToInterfaceOrientation 中失效:永远不会调用 sizeForItemAtIndexPath:。

希望你明白我的意思。如果您需要更多信息,请告诉我:)

最佳答案

这个答案来晚了,但接受的答案对我不起作用。我同情 OP 想要一个即发即弃的 UICollectionViewFlowLayout。我建议使 View Controller 中的布局无效实际上是最好的解决方案。

我想要一条水平滚动的单元格行,在 View 中居中,无论是纵向还是横向。

我继承了 UICollectionViewFlowLayout。

我覆盖了 prepareLayout 以重新计算插图,然后调用 [super prepareLayout]。

我覆盖了 collectionViewContentSize 的 getter 以确定内容大小是正确的。

即使边界随着重新定向而改变,布局也不会自行失效。

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// does the superclass do anything at this point?
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

// do whatever else you need before rotating toInterfaceOrientation

// tell the layout to recalculate
[self.collectionViewLayout invalidateLayout];
}

UICollectionViewFlowLayout 维护方向之间的滚动位置。同一个单元格只有是方形的才会居中。

关于ios - UICollectionViewFlowLayout 不会使方向更改的权利失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16341778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com