简体   繁体   English

Collection View不会更新Rotation的布局

[英]Collection View does not update layout at Rotation

This is the view of the app when it is in portrait mode. 这是应用程序处于纵向模式时的视图。 在此输入图像描述

When it is rotated to landscape mode it looks like this 当它旋转到横向模式时,它看起来像这样

在此输入图像描述

The view debugger shows that the UIWindow is not rotating as shown here 视图调试器显示UIWindow未旋转,如此处所示 在此输入图像描述

The UICollectionViewController is created via StoryBoard. UICollectionViewController是通过StoryBoard创建的。 I've tried subclassing UICollectionViewFlowLayout that implements shouldInvalidateLayoutForBoundsChange , but it does not fix my issue. 我已经尝试了实现shouldInvalidateLayoutForBoundsChange子类UICollectionViewFlowLayout ,但它并没有解决我的问题。

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

Please provide ideas of what to check next or requests for additional code to debug. 请提供下一步检查的内容或要求调试其他代码的请求。

Edit - As suggested by MirekE, I attempted to add constraints to the CollectionView but was unable. 编辑 - 正如MirekE所建议的那样,我试图向CollectionView添加约束但却无法使用。 All of the options for Editor->Pin are unavailable for the CollectionView . Editor-> Pin的所有选项都不适用于CollectionView 在此输入图像描述

Edit, response to Andrea - 编辑,回应安德里亚 -

I'm targeting iOS8.3. 我的目标是iOS8.3。 My understanding is that the main method called at rotation is viewWillTransitionToSize:withTransitionCoordinator: , which is from the UIContentContainer protocol . 我的理解是旋转时调用的主要方法是viewWillTransitionToSize:withTransitionCoordinator: ,它来自UIContentContainer 协议 I've added the following to my CollectionViewController, but same problem persists 我已将以下内容添加到我的CollectionViewController中,但同样的问题仍然存在

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

I don't know which iOS version you ara targeting, but let me suppose that you know the rotation process and methods called in the view controller while it's happening. 我不知道你的目标是哪个iOS版本,但我想你知道视图控制器中调用的旋转过程和方法正在发生。
In one of those methods you just need to call: 在其中一种方法中,您只需要调用:

[self.collectionView.collectionViewLayout invalidateLayout];

and maybe depending on your layout -reloadData 并且可能取决于你的布局-reloadData
No need to subclass. 不需要子类。


EDIT 编辑

I use this method, I guess that is not working because you should relayout the collection after it has resized: 我使用这种方法,我想这不起作用,因为你应该在调整大小后重新传输集合:

- (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];
    }];

}


What I'm doing attach the layout invalidation process to the animation process. 我正在做什么将布局失效过程附加到动画过程。

If you have custom implementation of UICollectionViewLayout, try to override methods: 如果您有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()

}

Cheers ! 干杯!

It looks like you just drag the collection view on the canvas, but did not add any constraints. 看起来您只是在画布上拖动集合视图,但没有添加任何约束。 So when you rotate the device, the size does not change. 因此,当您旋转设备时,大小不会改变。

In the storyboard select the collection view, then click on the Pin icon at the bottom of Xcode and add constraints for the top, left, bottom and right margins. 在故事板中选择集合视图,然后单击Xco​​de底部的Pin图标并添加顶部,左侧,底部和右侧边距的约束。 After you do that, the collection view should resize on rotation. 执行此操作后,集合视图应在旋转时调整大小。

The problem was that the collectionView outlet was not properly set. 问题是collectionView插座没有正确设置。 After setting the collectionView , all works properly. 设置collectionView ,一切正常。

collectionView outlet not set: collectionView出口没有设置:

在此输入图像描述

collectionView outlet set: collectionView插座集:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM