2013-11-07 3 views
8

У меня есть UIViewController, который содержит UICollectionView. Но UICollectionView не заполняет все UIViewController.Как установить положение ячеек на UICollectionView

Я нахожу, что есть пространство, высота которого равна высоте NavigationBar между ячейкой и верхним краем UICollectionView. Я не знаю, как установить положение ячейки (0,0) в UICollectionView. (Как это, пространство в красном прямоугольнике)

enter image description here

Я нашел эту ссылку How do I set the position of a UICollectionViewCell? И я подкласс UICollectionViewFlowLayout (следующий мой код)

MZMCollectionViewFlowLayout.h

#import <UIKit/UIKit.h> 

@interface MZMCollectionViewFlowLayout : UICollectionViewFlowLayout 

@end 

MZMCollectionViewFlowLayout.m

#import "MZMCollectionViewFlowLayout.h" 

@implementation MZMCollectionViewFlowLayout 

- (CGSize)collectionViewContentSize 
{ 
    return [super collectionViewContentSize]; 
} 

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
{ 
    return [super layoutAttributesForElementsInRect:rect]; 
} 

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"MZMCollectionViewFlowLayout layoutAttributesForItemAtIndexPath"); 
    if (indexPath.section == 0 && indexPath.row == 1) // or whatever specific item you're trying to override 
    { 
     UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 
     layoutAttributes.frame = CGRectMake(0,0,100,100); // or whatever... 
     return layoutAttributes; 
    } 
    else 
    { 
     return [super layoutAttributesForItemAtIndexPath:indexPath]; 
    } 
} 

@end 

и использовать его как это:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    // hide the tool bar 
    [self.navigationController setToolbarHidden:YES animated:YES]; 

    // set title 
    self.navigationItem.title = @"User Album"; 

    [self.userAlbumView setCollectionViewLayout:[[MZMCollectionViewFlowLayout alloc] init]]; 
} 

Но это не работает. Журнал NSLog (@ "MZMCollectionViewFlowLayout layoutAttributesForItemAtIndexPath"); не отображается. И пробел все еще там.

Спасибо за помощь!

+0

Я считаю это [ссылка] (http://stackoverflow.com/questions/13040542/subclassing-uicollectionviewlayout-and-assign-to-uicollectionview), который отвечает на мой вопрос ** layoutAttributesForItemAtIndexPath: не работает find **. Но пространство между ячейкой и верхним краем все еще существует. –

+0

Возможный дубликат [UICollectionView добавляет верхнее поле] (http://stackoverflow.com/questions/19411442/uicollectionview-adds-top-margin) – jrturton

+0

Связанная проблема? http://stackoverflow.com/questions/24916006/in-a-uicollectionview-the-cells-will-not-sit-on-the-bottom – Fattie

ответ

1

Ответьте на вопрос.

Я напечатал каждую информацию UICollectionViewLayoutAttribute в layoutAttributesForElementsInRect: и нашел то, что мне нужно, чтобы изменить frame.orgin.y

Ниже мой код:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
{ 
    NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; 
    NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:[attributes count]]; 

    for (int i=0; i< [attributes count]; i++) { 
     UICollectionViewLayoutAttributes *attr = (UICollectionViewLayoutAttributes *)[attributes objectAtIndex:i]; 

     // the key code "attr.frame.origin.y - 63" 
     [attr setFrame:CGRectMake(attr.frame.origin.x, attr.frame.origin.y - 63, attr.bounds.size.width, attr.bounds.size.height)]; 

     //NSLog(@"attr h=%f w=%f x=%f y=%f", attr.bounds.size.height, attr.bounds.size.width, attr.frame.origin.x, attr.frame.origin.y); 

     [result addObject:attr]; 

    } 

    return result; 
} 

то он работает отлично.

16

FYI, это отвечает более правильно здесь: UICollectionView adds top margin

Проблема заключается в контроллер вид автоматически регулируя вид прокрутки врезки. Это можно отключить либо в коде, либо в IB. В коде просто установить:

self.automaticallyAdjustsScrollViewInsets = NO; 
+0

Это правильный ответ. – CarmeloS

+1

См. [Здесь] (http://stackoverflow.com/a/21112952/211292), как установить это с помощью IB. – ThomasW

Смежные вопросы