在合适的时间获取UICollectionview的边界/框架

时间:2015-10-23 10:01:32

标签: ios objective-c autolayout uicollectionview

我正在编写一个应用程序(IOS,Objective-C),它以包含按钮的Collectionview开头,具体取决于按钮,我将推送下一个CollectionViewcontroller或TableviewController来显示数据。

我正在使用下面列出的功能来调整单元格的大小到屏幕(特别是从纵向转为横向等)。

我有以下问题。

当我从这个collectionview推送tableviewcontroller并返回到collectionview时,计算正确完成,并且collectionview看起来应该如此。

但是当从另一个集合视图返回时计算错误(即宽度太高),目前正在测试Iphone 5s并且宽度设置为低于400但是从另一个集合视图返回之后整个视图的宽度以560记录。

那么如何为我的Collectionview获取正确的边界/框架呢?

我还应该提到我使用的是sizeclass" Any / Any"并且我的collectionviewController派生自UICollectionviewController以容纳AdbannerView(在另一个视图中嵌入collectionview)

任何帮助都会受到高度赞赏,下面的代码片段来自某些博客并且效果非常好,但只有在堆栈中没有其他Collectionview时才会这样。

    - (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout*)collectionViewLayout
     sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    UICollectionViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"WNCOllectionviewcell" owner:self options:nil] objectAtIndex:0];
    cell.frame = CGRectMake(0,0,CGRectGetWidth(_WNCollectionView.bounds)*0.4, CGRectGetHeight(cell.frame));
   CGFloat desiredHeight = (CGRectGetHeight(_WNCollectionView.bounds) / _collectionImages.count) * 1.45;
    NSLog (@"Height %.2f", CGRectGetHeight(_WNCollectionView.frame));
    NSLog(@"Width %.2f",CGRectGetWidth(_WNCollectionView.frame));
    return CGSizeMake(CGRectGetWidth(collectionView.bounds)*0.4, desiredHeight);

    } 

CollectionViewController声明

#import <UIKit/UIKit.h>

@import iAd;

@interface WNCollectionViewController : UIViewController      <UICollectionViewDelegate, UICollectionViewDataSource, ADBannerViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *WNCollectionView;
@end

1 个答案:

答案 0 :(得分:1)

我自己解决了这个问题,这个问题对任何好心的读者都无法解决,所以请接受我的道歉。

在我的设置中,我有CollectionViewController A调用CollectionViewController B.两者都有NavigationControllers。我错误地将B的输出与CollectionViewController A连接,而不是与A&#39的Navigationcontroller连接,后者产生了所有有趣的结果。以正确的方式连接(通过navigationcontroller)完成了这项工作。