gpt4 book ai didi

iOS - 带有内容插入的粘性标题 - 标题 View 不像单元格那样滚动

转载 作者:行者123 更新时间:2023-11-29 05:57:56 25 4
gpt4 key购买 nike

我们尝试像 map 上的列表一样滚动UICollectionView。不要将其与捕捉到某个点(如低、中和高)的底板混淆。 CollectionView 的 flowlayout 属性已启用 sectionHeadersPinToVisibleBounds。我附上了示例项目供您引用。当用户滚动时,有什么方法可以将标题 View 移动到 Collection View 的顶部吗?

Here是示例项目

我需要进行必要的改变才能进入这种状态:

let layout = UICollectionViewFlowLayout()
layout.sectionHeadersPinToVisibleBounds = true
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0

let collectionView = UICollectionView(frame: .zero,
collectionViewLayout: layout)


override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()

collectionView.contentInset = UIEdgeInsets(top: drawerHeight, left: 0, bottom: 0, right: 0)
}

这是您将看到的屏幕截图: enter image description here

红色 View 是固定的标题 View 。我是否需要自定义布局来在用户滚动时更新其位置?

最佳答案

我受此启发编写了一个自定义的 StickyHeaderLayout post 。这是我的错误的修复:

class StickyHeaderLayout: UICollectionViewFlowLayout {

override init() {
super.init()
configureLayout()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configureLayout()
}

private func configureLayout() {
self.sectionFootersPinToVisibleBounds = true
self.sectionHeadersPinToVisibleBounds = true
minimumLineSpacing = 0
minimumInteritemSpacing = 0
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: rect) else { return nil }

for attribute in attributes {
adjustAttributesIfNeeded(attribute)
}
return attributes
}

override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let attributes = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) else { return nil }
adjustAttributesIfNeeded(attributes)
return attributes
}

func adjustAttributesIfNeeded(_ attributes: UICollectionViewLayoutAttributes) {
switch attributes.representedElementKind {
case UICollectionView.elementKindSectionHeader?:
adjustHeaderAttributesIfNeeded(attributes)
default:
break
}
}

private func adjustHeaderAttributesIfNeeded(_ attributes: UICollectionViewLayoutAttributes) {
guard let collectionView = collectionView else { return }
guard attributes.indexPath.section == 0 else { return }

if collectionView.contentOffset.y <= 0 {
attributes.frame.origin.y = 0
} else {
attributes.frame.origin.y = collectionView.contentOffset.y
}
}
}

关于iOS - 带有内容插入的粘性标题 - 标题 View 不像单元格那样滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54964574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com