简体   繁体   English

如何在UICollectionView上设置单元格的位置

[英]How to set the position of cells on UICollectionView

I have an UIViewController which contains a UICollectionView. 我有一个包含UICollectionView的UIViewController。 But the UICollectionView does not fill all the UIViewController. 但是UICollectionView并没有填满所有的UIViewController。

I find that there is space whose height equals the height of NavigationBar between the cell and the top edge of the UICollectionView. 我发现有一些空间的高度等于单元格和UICollectionView顶部边缘之间的NavigationBar的高度。 I don't know how I can set the cell position to (0,0) in the UICollectionView. 我不知道如何在UICollectionView中将单元格位置设置为(0,0)。 (like this, the space is in the red rectangle) (像这样,空间是红色矩形)

在此输入图像描述

I found this link How do I set the position of a UICollectionViewCell? 我找到了这个链接如何设置UICollectionViewCell的位置? And I subclass UICollectionViewFlowLayout (the following are my code) 我将UICollectionViewFlowLayout子类化(以下是我的代码)

MZMCollectionViewFlowLayout.h MZMCollectionViewFlowLayout.h

#import <UIKit/UIKit.h>

@interface MZMCollectionViewFlowLayout : UICollectionViewFlowLayout

@end

MZMCollectionViewFlowLayout.m 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"); 日志NSLog(@“MZMCollectionViewFlowLayout layoutAttributesForItemAtIndexPath”); doesn't show. 不显示。 And the blank is still there. 空白仍在那里。

Thanks for any help! 谢谢你的帮助!

FYI, this is answered more correctly here: UICollectionView adds top margin 仅供参考,这里答案更正确: UICollectionView增加了上边距

The problem is the view controller is automatically adjusting scroll view insets. 问题是视图控制器自动调整滚动视图插入。 That can be turned off either in code or IB. 这可以在代码或IB中关闭。 In code just set: 在刚设置的代码中:

self.automaticallyAdjustsScrollViewInsets = NO;

Answer myself question. 回答自己的问题。

I printed every UICollectionViewLayoutAttribute information in layoutAttributesForElementsInRect: and found what I need is to change the frame.orgin.y 我在layoutAttributesForElementsInRect中打印了每个UICollectionViewLayoutAttribute信息:并且发现我需要的是更改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. 然后它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM