Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the position of cells on UICollectionView

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!

like image 704
Preston Cheung Avatar asked Nov 07 '13 10:11

Preston Cheung


People also ask

What is UICollectionView flow layout?

A layout object that organizes items into a grid with optional header and footer views for each section.

What is the difference between UITableView and UICollectionView?

For the listing details of each item, people use UITableView because it shows more info on each item. The UICollectionView class manages an ordered collection of data items and presents them using customizable layouts.

Do CollectionView use cells?

Like a table view, a collection view is a UIScrollView subclass. UICollectionViewCell: This is similar to a UITableViewCell in a table view. These cells make up the view's content and are subviews to the collection view. You can create cells programmatically or inside Interface Builder.

How does UICollectionView work?

From apple's documentation, UICollectionView is: An object that manages an ordered collection of data items and presents them using customizable layouts. The name and definition makes it clear, it is a way to display a Collection of UI Views in our app. The individual view is referred as a Cell.


2 Answers

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;
like image 137
JCP Avatar answered Sep 27 '22 18:09

JCP


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.

like image 30
Preston Cheung Avatar answered Sep 27 '22 20:09

Preston Cheung