粘性 UICollectionView Header F#

Sticky UICollectionView Header F#

我有一个 UICollectionViewController 布局如下:

所以我们有一个 CollectionView 和一个包含可重用视图的 Header。

我为可重用视图实现了以下方法:

override x.GetViewForSupplementaryElement(collectionView : UICollectionView, elementKind : NSString, indexPath : NSIndexPath) =
    let headerView = (collectionView.DequeueReusableSupplementaryView(elementKind,new NSString("HeaderView"),indexPath))
    headerView

我唯一的问题是,我不希望 header 与 UICollectionView 一起滚动,我希望它始终保持在 [=13] 顶部的固定位置=].我怎样才能做到这一点?

这可以使用以下代码完成:

let layout = x.CollectionView.CollectionViewLayout :?> UICollectionViewFlowLayout
layout.SectionHeadersPinToVisibleBounds <- true

改编自UICollectionView sticky header in swift

为了消除 collectionView 顶部的白色 space,请执行以下操作:

x.AutomaticallyAdjustsScrollViewInsets <- false
x.CollectionView.ContentInset <- UIEdgeInsets(Conversions.nfloat(-44),Conversions.nfloat(0), Conversions.nfloat(0), Conversions.nfloat(0))

请注意,-44 的值专门用于 iPhone X,其他型号可能有所不同。

Conversions 用于转换为nfloat。这是执行此转换的代码:

let inline nfloat (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> nfloat) x)

最后,如果您希望消除 UICollectionView 顶部的垂直弹跳,请将以下代码添加到您的 UICollectionViewController 中:

override x.Scrolled(scrollView : UIScrollView) = 
    if (scrollView.ContentOffset.Y < Conversions.nfloat(0)) then 
        scrollView.ContentOffset <- CGPoint(scrollView.ContentOffset.X, Conversions.nfloat(0))