UICollectionView标题在水平滚动方向模式下的位置与stream程布局

我有ios UICollectionView与水平滚动方向的stream布局。 在这种情况下,典型的标题位置在单元格的左侧。

我想在部分单元格的顶部添加标题

你有什么想法我怎么能做到这一点?

我认为你可以得到你需要使用标题的标准行为。

设置它(可能在viewDidLoad中):

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width, 30); // other setup [self.collectionView setCollectionViewLayout:flowLayout]; 

然后回答一个标题视图:

 #define MY_HEADER_LABEL_TAG 128 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath]; UILabel *label = (UILabel *)[headerView viewWithTag:MY_HEADER_LABEL_TAG]; if (!label) { label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)]; label.tag = MY_HEADER_LABEL_TAG; label.font = [UIFont boldSystemFontOfSize:12]; label.textColor = [UIColor darkGrayColor]; [headerView addSubview:label]; } label.text = [NSString stringWithFormat:@"Section %d", indexPath.section]; return headerView; } 

我使用DateFlowLayout解决了这个问题。

看看我如何configuration它在这个其他的答案 。