У меня есть UIViewController, который содержит UICollectionView. Но UICollectionView не заполняет все UIViewController.Как установить положение ячеек на UICollectionView
Я нахожу, что есть пространство, высота которого равна высоте NavigationBar между ячейкой и верхним краем UICollectionView. Я не знаю, как установить положение ячейки (0,0) в UICollectionView. (Как это, пространство в красном прямоугольнике)
Я нашел эту ссылку 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"); не отображается. И пробел все еще там.
Спасибо за помощь!
Я считаю это [ссылка] (http://stackoverflow.com/questions/13040542/subclassing-uicollectionviewlayout-and-assign-to-uicollectionview), который отвечает на мой вопрос ** layoutAttributesForItemAtIndexPath: не работает find **. Но пространство между ячейкой и верхним краем все еще существует. –
Возможный дубликат [UICollectionView добавляет верхнее поле] (http://stackoverflow.com/questions/19411442/uicollectionview-adds-top-margin) – jrturton
Связанная проблема? http://stackoverflow.com/questions/24916006/in-a-uicollectionview-the-cells-will-not-sit-on-the-bottom – Fattie