【发布时间】:2015-10-23 10:01:31
【问题描述】:
我正在编写一个应用程序(IOS,Objective-C),它以包含按钮的 Collectionview 开头,根据按钮,我将推动下一个 CollectionViewcontroller 或 TableviewController 来呈现数据。
我正在使用下面列出的功能来调整单元格的大小以适应屏幕(尤其是从纵向转为横向等时)
我有以下问题。
当我从这个 collectionview 推送一个 tableviewcontroller 并返回到 collectionview 时,计算已正确完成,并且 collectionview 看起来应该如此。
但是当从另一个collectionview返回时计算是错误的(即宽度太高了),目前在Iphone 5s上测试并且宽度设置为低于400但是从另一个collectionview返回后整个视图的宽度记录为 560。
那么如何为我的 Collectionview 获得正确的边界/框架?
我还应该提到我正在使用 sizeclass "Any/Any",并且我的 collectionviewController 是从 UICollectionviewController 派生的,以便容纳 AdbannerView(将 collectionview 嵌入另一个视图中)
任何帮助都将不胜感激,下面的 sn-p 取自一些博客,效果非常好,但仅当堆栈中没有另一个 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
【问题讨论】:
-
你确定
collectionView和_WNCollectionView是同一个实例吗?如果不是,您打印的值错误 -
添加了 CollectionViewController 的声明...以便澄清
标签: ios objective-c autolayout uicollectionview