UICollectionView在启动时溢出

时间:2014-02-14 10:11:59

标签: ios cocoa-touch constraints collectionview

当我启动应用程序时,视图看起来像这样。 http://i.imgur.com/vHi2zj0.png

CollectionView基本上从底部溢出,这意味着即使我向下滚动也无法看到它的一部分。

但是,只要我将设备向左旋转然后再向后旋转(从纵向到横向再回到纵向),它就会转到我想要的样子。 http://i.imgur.com/QdlV7xB.png

有没有办法刷新viewDidLoad中的视图,以便它看起来像旋转两次后看起来一样?

CollectionViewController.m     #import“CollectionViewController.h”

@interface CollectionViewController () {
    NSArray * listImages;
}

@end

@implementation CollectionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.jpg"]]];
    // Do any additional setup after loading the view.
    listImages = [NSArray arrayWithObjects:@"7513-kirjatkuivumassa.jpg", @"kuppi.jpg", @"kuva1.jpg", @"juna-042.jpg", @"rautio-valamonruusut-helleaamuna-maalaus.jpg", @"pysähtynyt1.jpg", @"Screen-Shot-2013-02-20-at-21.07.38.jpg", @"sateenkaari.jpg", @"Screen-Shot-2013-02-21-at-17.04.22.jpg", @"moninaiset-e1391026376696.jpg", @"Tomperi+Metsä20111.jpg", @"3-shinot.jpg", @"Ulpukat.jpg", @"janne-e1391025808211.jpg", @"martikainen-240x240.jpg", @"takala-240x240.jpg", @"paanukallokaarme1.jpg", @"käsityök-240x240.jpg", @"kuvis-004.jpg", @"Se-on-hieno-2012-tammi-105x28x223.jpg", nil];
}

- (void)viewDidLayoutSubviews
{
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [listImages count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    UIImageView *listImageView = (UIImageView *)[cell viewWithTag:100];
    listImageView.image = [UIImage imageNamed:[listImages objectAtIndex:indexPath.row]];

    return cell;
}

CollectionViewController.h

#import <UIKit/UIKit.h>

@interface CollectionViewController : UIViewController

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

2 个答案:

答案 0 :(得分:0)

您需要在UICollectionView文件中创建对ViewController.h的引用。

执行以下操作:

1)打开助理编辑器。应自动选择ViewController.h,如果没有,请选择它。

2)Control-drag你的`UICollectionView进入你的.h文件。

3)弹出窗口会要求您命名您的视图。输入collectionView

4)将以下代码添加到.m文件

- (void)viewDidLayoutSubviews
{
    [self.collectionView reloadData];
}

答案 1 :(得分:0)

试试这个

[self.collectionView reloadData];