【发布时间】:2014-05-16 06:15:18
【问题描述】:
在iOS 7 中工作,如何在UICollectionView 中指定页眉和页脚框的位置?
我有一个自定义的UICollectionViewFlowLayout。我已经覆盖了
-(void)prepareLayout
-(NSArray*) layoutAttributesForElementsInRect:(CGRect)rect
-(UICollectionViewLayoutAttributes*) layoutAttributesForSupplementaryViewOfKind: (NSString*)kind atIndexPath:(NSIndexPath*)indexPath
我的问题是,我不确定如何指定标题位置。我已经指定了prepareLayout中存在一个header:
-(void)prepareLayout
{
[super prepareLayout];
boundsSize = self.collectionView.bounds.size;
midX = boundsSize.width / 2.0f;
curIndex = 0;
self.headerReferenceSize = CGSizeMake(CELL_SIZE, TITLE_HEIGHT);
self.footerReferenceSize = CGSizeMake(0, 0);
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.sectionInset = UIEdgeInsetsMake(TOP_INSET, LEFT_INSET, BOTTOM_INSET, RIGHT_INSET);
self.minimumLineSpacing = LINE_SPACING;
self.minimumInteritemSpacing = INTERIM_SPACING;
self.itemSize = CGSizeMake(CELL_SIZE, CELL_SIZE);
}
我只是不知道要设置的自定义 FlowLayout 的正确属性,因为似乎没有像“HeaderLocation”这样的东西可以设置为 LayoutAttributes 或布局对象本身。现在,当我希望它们出现在每个图像上方(水平滚动)时,它出现在我的图像的侧面/之间。
我尝试了以下方法:
-(UICollectionReusableView*) collectionView: (UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"**ViewForSupplementaryElementOfKind called***");
CGFloat centerX = collectionView.center.x;
CGFloat centerY = collectionView.center.y;
CGFloat titleWidth = [MyLayout titleWidth];
CGFloat titleHeight = [MyLayout titleHeight];
MyTitleView* titleView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:ImageTitleIdentifier forIndexPath:indexPath];
titleView.frame = CGRectMake(centerX - titleWidth/2.0,
0.0,
titleWidth,
titleHeight);
return titleView;
}
这不起作用。标题出现在上面与一堆其他标题重叠,然后当我开始滚动(水平)时,它们会跳回错误的位置,在图像之间水平而不是在上面。
PS> 请不要提出任何与 NIB 或 XIB 放置有关的建议。我使用的是 UICollectionView,而不是 UICollectionViewController,所以我实际上没有原型单元可以使用。布局完全以编程方式完成——仅从代码开始——所以我不能简单地打开 XIB 文件并调整文本框的位置。
【问题讨论】:
标签: ios objective-c uicollectionview flowlayout