将UICollectionView标头内容扩展到第一个单元格下方

时间:2018-04-03 17:22:09

标签: ios swift uicollectionview

以下是我想要实现的效果,将标题的内容扩展到第一个单元格以下。我尝试将图像大小设置为大于标题,并将clipsToBounds设置为false。但是,图像位于单元格顶部并覆盖它。有没有办法将图像移动到单元格下方?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过在willDisplayXXX方法中设置图层的zPosition来完成此操作。

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
{
    view.layer.zPosition = 0.0;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    cell.layer.zPosition = 99 + indexPath.row;
}

同样,您可以使用UITableViewwillDisplayHeaderView方法在willDisplayCell中完成此操作。基本上,将标题设置为较低的值并将单元格设置为较高的值,并且应将单元格放在标题的前面。

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    view.layer.zPosition = 0.0;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(MyCustomCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.layer.zPosition = 99 + indexPath.row;
}