Pre*_*ung 9 ios uicollectionview uicollectionviewlayout
我有一个包含UICollectionView的UIViewController.但是UICollectionView并没有填满所有的UIViewController.
我发现有一些空间的高度等于单元格和UICollectionView顶部边缘之间的NavigationBar的高度.我不知道如何在UICollectionView中将单元格位置设置为(0,0).(像这样,空间是红色矩形)

我找到了这个链接如何设置UICollectionViewCell的位置?我将UICollectionViewFlowLayout子类化(以下是我的代码)
MZMCollectionViewFlowLayout.h
#import <UIKit/UIKit.h>
@interface MZMCollectionViewFlowLayout : UICollectionViewFlowLayout
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
- (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]];
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.日志NSLog(@"MZMCollectionViewFlowLayout layoutAttributesForItemAtIndexPath"); 不显示.空白仍在那里.
谢谢你的帮助!
JCP*_*JCP 16
仅供参考,这里答案更正确:UICollectionView增加了上边距
问题是视图控制器自动调整滚动视图插入.这可以在代码或IB中关闭.在刚设置的代码中:
self.automaticallyAdjustsScrollViewInsets = NO;
Run Code Online (Sandbox Code Playgroud)
回答我自己的问题。
我打印了layoutAttributesForElementsInRect:中的每个UICollectionViewLayoutAttribute信息,发现我需要的是更改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;
}
Run Code Online (Sandbox Code Playgroud)
然后就可以正常工作了。
| 归档时间: |
|
| 查看次数: |
13354 次 |
| 最近记录: |