是否可以在侧面为 UICollectionView 创建补充侧视图而不是作为 header 或页脚?

Is it possible to create a supplementary side view for a UICollectionView on the side instead of as a header or footer?

我正在尝试创建一个设置,其中我有一个顶部带有 header 的 UICollectionView,然后是侧面的另一个补充视图。我不能只将补充视图放在 collectionView 之外,因为它与 collectionView 内部的 collectionView 单元格共享 space,而且我还希望它与它一起滚动。它应该是这样的:

我一直在想,我能想到的一个解决方案是制作一个大的 collectionView,补充侧视图和右边的视图作为它的单元格,header 作为它的 header。但是,我听说将 collectionView 放在 collectionView 中并不是很好,特别是如果滚动方向相同,而且我还想在滚动时动态加载更多单元格,所以在那种情况下我必须动态保持调整单元格大小。

这种配置是否可以优雅的方式实现?谢谢!

这是有可能实现的。有一种简单的方法,设置 Supplementary ViewFrame 并在 SectionInset 的左侧添加 UIEdgeInsets .

代码如下:

flowLayout = new UICollectionViewFlowLayout (){
    HeaderReferenceSize = new CGSize(50, 50),
    SectionInset = new UIEdgeInsets (20,65,20,20),
    ScrollDirection = UICollectionViewScrollDirection.Vertical,
    MinimumInteritemSpacing = 10, // minimum spacing between cells
    MinimumLineSpacing = 10 // minimum spacing between rows if ScrollDirection is Vertical or between columns if Horizontal
};

自定义 UICollectionReusableView class :

public class Header : UICollectionReusableView
{
    UILabel label;

    public string Text {
        get {
            return label.Text;
        }
        set {
            label.Text = value;
            SetNeedsDisplay ();
        }
    }

    [Export ("initWithFrame:")]
    public Header (CGRect frame) : base (frame)
    {
        BackgroundColor = UIColor.Red;
        label = new UILabel (){Frame = new CGRect (0,80,50,500), BackgroundColor = UIColor.Yellow};
        label.Lines = 5;
        AddSubview(label);
    }
}

效果:

以上代码根据this official sample修改.