在Xcode中向UICollectionView添加子视图

5

我正在使用UICollectionView,将设备中的照片添加到UICollectionViewCell中,现在我想在UICollectionView中创建子视图。请建议如何在UICollectionViews中添加subview。需要任何代码或链接。


https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionViewCell_class/Reference/Reference.html#//apple_ref/occ/instp/UICollectionViewCell/contentView - Sam Fischer
请让我明确一下,您是想在UICollectionView中添加子视图还是在UICollectionViewCell中添加子视图? - Rugmangathan
是的,我认为不需要子视图。您可以根据需要完全自定义集合视图单元格。 - Ganapathy
1
我想在UICollectionViewCell中添加子视图。 - user241641
请告诉我如何自定义CollectionViewCell。 - user241641
1个回答

18

添加一个子视图就像这样简单,

//Create subview    
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.collectionView.frame.size.width, 20)];
//Add
[collectionView addSubview:subview];
//Bring to front so that it does not get hidden by cells
[collectionView bringSubviewToFront:subview];
//This will shift collectionView content down by 20px at top
[collectionView setContentInset:UIEdgeInsetsMake(subview.frame.size.height, 0, 0, 0)];

以上代码将在collectionView顶部添加一个子视图。如果这不是您想要的,请在问题中提供更多细节。


在iOS 8中,插入空间将位于子视图顶部以上。 - jAckOdE
@jAckOdE,你有没有在iOS8上实现这个的想法? - Andrei Radulescu
1
将视图添加到您的集合视图中,并观察集合视图的内容偏移量,然后相应地更新您的子视图框架。 - jAckOdE
@jAckOdE,你能分享一下内容偏移的示例代码吗? - Afzaal Ahmad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接