2013-04-17 12 views
45

私は現在UICollectionViewDataSourceで設定されたUICollectionViewを持っていますが、現在6つの項目があります。 これは、画面を埋めるのに必要な数よりも少ない数です。問題は、画面を埋めるのに十分なアイテムがある場合(10,20でテスト済み)、コレクションビューがスクロールするだけであるということです。 アイテムを表示しているときに、私は取得しようとしているこのバウンスアニメーションを実行することもなく、修正されました。UICollectionViewはスクロールしません

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCollectionViewData) name:UIDocumentStateChangedNotification object:nil]; 

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; 
    flowLayout.itemSize = CGSizeMake(160, 100); 
    flowLayout.minimumInteritemSpacing = 0; 
    flowLayout.minimumLineSpacing = 0; 

    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; 
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"]; 
    self.collectionView.delegate = self; 
    self.collectionView.dataSource = self; 
    self.collectionView.bounces = YES; 
    [self.view addSubview:self.collectionView]; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return [self.collectionViewData count]; 
} 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 

    Expense *expense = [self.collectionViewData objectAtIndex:indexPath.row]; 

    UILabel *label = [[UILabel alloc]initWithFrame:cell.bounds]; 
    label.text = expense.value; 
    label.backgroundColor = [UIColor clearColor]; 
    label.font = [UIFont fontWithName:@"Miso-Bold" size:30]; 
    label.textAlignment = NSTextAlignmentCenter; 
    [cell addSubview:label]; 

    cell.backgroundColor = [UIColor colorWithRed:1 - (indexPath.row/30.0f) green:0 blue:1 alpha:1]; 

    return cell; 
} 

ありがとうございました!

答えて

151

bouncesの名前にもかかわらず、設定する権利プロパティではありません。また、alwaysBounceVerticalおよび/またはalwaysBounceHorizontalを設定する必要があります。ドキュメントから:

このプロパティはYESとバウンスに設定されている場合はYES、垂直ドラッグは、コンテンツがスクロールビューの境界よりも小さい場合でもを許可されています。デフォルト値はNOです。


注IBで紛らわしい名前..コレクションビュー「バウンス」と「バウンス垂直方向」の属性インスペクタで、ストーリーボードでhttps://stackoverflow.com/a/18391029/294884

+0

これは正解です - OPはこれを答えとして受け入れるべきです。 –

+0

素晴らしいヒント – Fattie

+0

ありがとう、それは魅力のように動作します:) – evya

4

の高さをUIViewのサイズに設定すると、スクロールの問題が無効になります。 UICollectionViewの高さが568ピクセルの場合、その中に568ピクセル以上のコンテンツが含まれている場合はスクロールする必要があります。あなたはそれが含まれているビューの高さに設定するべきです(幅と同じです)。

お手伝いします。

+0

UICollectionViewのエッジをスーパービューのエッジと同じに保つためにIBに制約を加えました – Colin

9

をチェックする必要があります。

関連する問題