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

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

问题描述

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



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

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



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

 项目宽度必须小于UICollectionView的宽度减去该部分的左右值。 
请检查代表返回的值。

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



希望你理解我的意思。如果您需要其他信息,请告诉我们:)

解决方案

一种解决方案是使用KVO并监听对框架的更改您的收藏视图的超级视图。调用-reloadData将起作用并消除警告。这里有一个潜在的时间问题...

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

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

// KVO方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)对象更改:(NSDictionary *)更改上下文:(void *)context
{
[collectionView_ reloadData];
}


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

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.

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.

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 :)

解决方案

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天全站免登陆