UICollectionViewFlowLayout 在方向更改时不会失效 [英] UICollectionViewFlowLayout not invalidating right on orientation change

查看:10
本文介绍了UICollectionViewFlowLayout 在方向更改时不会失效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UICollectionViewFlowLayout 的 UICollectionView.我还实现了 UICollectionViewDelegateFlowLayout 协议.

i have a UICollectionView with a UICollectionViewFlowLayout. I also implement the UICollectionViewDelegateFlowLayout protocol.

在我的数据源中,我有一堆 UIViewControllers 响应自定义协议,所以我可以询问它们的大小和其他一些东西.

In my datasource i have a bunch of UIViewControllers which respond to a custom protocol so i can ask for their size and some other stuff.

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

In in the FlowLayout delegate when it asks for the sizeForItemAtIndexPath:, i return the item size which i get from my protocol. The ViewControllers which implement my protocol return a different item size depending on the orientation.

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

Now if i change the device orientation from portrait to landscape theres no problem (Items are larger in landscape) but if i change it back, i get this warning:

    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: 永远不会被调用.

It still works but i don't like it to get warnings so maybe you can tell me what i am doing wrong. Also there is another problem. If i don't tell my collectionViews collectionViewLayout to invalidate in willAnimateRotationToInterfaceOrientation: the sizeForItemAtIndexPath: is never called.

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

Hope you understand what i mean. If you need additional information let me know :)

推荐答案

一种解决方案是使用 KVO 并监听您的集合视图的父视图框架的变化.调用 -reloadData 将起作用并消除警告.这里有一个潜在的时间问题......

One solution is to use KVO and listen for a change to the frame of the superview for your collection view. Calling -reloadData will work and get rid of the warnings. There is an underlying timing issue here...

// -loadView
[self.view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];

// -dealloc
[self.view removeObserver:self forKeyPath:@"frame"];

// KVO Method
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [collectionView_ reloadData];
}

这篇关于UICollectionViewFlowLayout 在方向更改时不会失效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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