Collection View不会更新Rotation的布局

Idr*_*Idr 7 objective-c ios uicollectionview uicollectionviewlayout

这是应用程序处于纵向模式时的视图. 在此输入图像描述

当它旋转到横向模式时,它看起来像这样

在此输入图像描述

视图调试器显示UIWindow此处未旋转在此输入图像描述

UICollectionViewController是通过StoryBoard创建的.我已经尝试了子类化UICollectionViewFlowLayout实现shouldInvalidateLayoutForBoundsChange,但它不能解决我的问题.

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    CGRect oldBounds = self.collectionView.bounds;
    if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) {
        return YES;
    }
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

请提供下一步检查的内容或要求调试其他代码的请求.

编辑 - 正如MirekE所建议的那样,我试图添加约束CollectionView但却无法.Editor-> Pin的所有选项都不可用CollectionView. 在此输入图像描述

编辑,回应安德里亚 -

我的目标是iOS8.3.我的理解是旋转时调用的主要方法viewWillTransitionToSize:withTransitionCoordinator:是来自UIContentContainer 协议.我已将以下内容添加到我的CollectionViewController中,但同样的问题仍然存在

-(void)viewWillTransitionToSize:(CGSize)size
      withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator {
    [self.collectionView.collectionViewLayout invalidateLayout];
    [self.collectionView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

And*_*rea 10

我不知道你的目标是哪个iOS版本,但我想你知道视图控制器中调用的旋转过程和方法正在发生.
在其中一种方法中,您只需要调用:

[self.collectionView.collectionViewLayout invalidateLayout];
Run Code Online (Sandbox Code Playgroud)

并且可能取决于您的布局-reloadData
无需子类.


编辑

我使用这种方法,我想这是行不通的,因为你应该重新调整后重新集中它:

- (void) willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];


    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self.collectionView.collectionViewLayout invalidateLayout];            
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self.collectionView reloadData];
    }];

}
Run Code Online (Sandbox Code Playgroud)


我正在做什么将布局失效过程附加到动画过程.


小智 5

如果您有UICollectionViewLayout的自定义实现,请尝试覆盖方法:

override public func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
    let bounds = self.collectionView!.bounds;
    return ((CGRectGetWidth(newBounds) != CGRectGetWidth(bounds) ||
        (CGRectGetHeight(newBounds) != CGRectGetHeight(bounds))));
}

override public func invalidateLayout() {
    cache.removeAll()  // remove layout attributes ans settings if they exist
    super.invalidateLayout()

}
Run Code Online (Sandbox Code Playgroud)

干杯!


Idr*_*Idr 1

问题是collectionView插座设置不正确。设置好后collectionView,一切正常。

collectionView插座未设置:

在此输入图像描述

collectionView插座组:

在此输入图像描述