9

I have an UIViewController which contains a UICollectionView. But the UICollectionView does not fill all the UIViewController.

I find that there is space whose height equals the height of NavigationBar between the cell and the top edge of the UICollectionView. I don't know how I can set the cell position to (0,0) in the UICollectionView. (like this, the space is in the red rectangle)

enter image description here

I found this link How do I set the position of a UICollectionViewCell? And I subclass UICollectionViewFlowLayout (the following are my code)

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

and using it like this:

- (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]];
}

But it does not work. The log NSLog(@"MZMCollectionViewFlowLayout layoutAttributesForItemAtIndexPath"); doesn't show. And the blank is still there.

Thanks for any help!

Community
  • 1
  • 1
Preston Cheung
  • 175
  • 1
  • 2
  • 12
  • I find this [link](http://stackoverflow.com/questions/13040542/subclassing-uicollectionviewlayout-and-assign-to-uicollectionview) which answers my question **layoutAttributesForItemAtIndexPath: does not work find**. But the space between cell and the top edge is still there. – Preston Cheung Nov 07 '13 at 11:29
  • possible duplicate of [UICollectionView adds top margin](http://stackoverflow.com/questions/19411442/uicollectionview-adds-top-margin) – jrturton May 07 '14 at 08:55
  • related problem? http://stackoverflow.com/questions/24916006/in-a-uicollectionview-the-cells-will-not-sit-on-the-bottom – Fattie Jul 23 '14 at 16:42

2 Answers2

17

FYI, this is answered more correctly here: UICollectionView adds top margin

The problem is the view controller is automatically adjusting scroll view insets. That can be turned off either in code or IB. In code just set:

self.automaticallyAdjustsScrollViewInsets = NO;
Community
  • 1
  • 1
JCP
  • 183
  • 1
  • 5
1

Answer myself question.

I printed every UICollectionViewLayoutAttribute information in layoutAttributesForElementsInRect: and found what I need is to change the frame.orgin.y

following is my code:

- (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;
}

then it works fine.

Preston Cheung
  • 175
  • 1
  • 2
  • 12