gpt4 book ai didi

ios - collectionview global header 不会将单元格向下推而是保持在顶部

转载 作者:行者123 更新时间:2023-12-05 06:07:26 25 4
gpt4 key购买 nike

我有一个 Collection View 标题(蓝色)和单元格(红色)。我希望能够以编程方式显示/隐藏标题,但是当我以编程方式显示标题时,它会出现在单元格的顶部(或使 ScrollView 下降一点)。我希望标题将整个 ScrollView 向下推,这样我就不必在单击“切换标题”后向上滚动。

enter image description here

我非常努力地为下面的问题重现最少的代码。请分享任何见解。

struct Section: Hashable {
var items: [Int]
}

class ViewController: UIViewController {

var collectionView: UICollectionView!
var dataSource: UICollectionViewDiffableDataSource<Section, Int>?

var showHeader = true

override func viewDidLoad() {
super.viewDidLoad()

collectionView = UICollectionView(frame: view.frame, collectionViewLayout: createCompositionalLayout())
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
collectionView.backgroundColor = .systemBackground

collectionView.register(CellView.self, forCellWithReuseIdentifier: "cellId")
collectionView.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "headerId")

view.addSubview(collectionView)

collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

createDataSource()
addData()
}

func createCompositionalLayout() -> UICollectionViewLayout {
// layout for cell
let layout = UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
layoutItem.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0)
let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(200))
let layoutGroup = NSCollectionLayoutGroup.vertical(layoutSize: layoutGroupSize, subitems: [layoutItem])
let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
return layoutSection
}

let config = UICollectionViewCompositionalLayoutConfiguration()

if showHeader {
let layoutSectionHeader = createGlobalHeader()
config.boundarySupplementaryItems = [layoutSectionHeader]
}

layout.configuration = config
return layout
}

func createGlobalHeader() -> NSCollectionLayoutBoundarySupplementaryItem {
let layoutSectionHeaderSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50))

let layoutSectionHeader = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: layoutSectionHeaderSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
layoutSectionHeader.pinToVisibleBounds = true

return layoutSectionHeader
}

func createDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section, Int>(collectionView: collectionView) { collectionView, indexPath, item in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? CellView else { fatalError("Unable to dequeue ") }
cell.button.addTarget(self, action: #selector(self.onButtonClick), for: .touchUpInside)
return cell
}

dataSource?.supplementaryViewProvider = { (collectionView, kind, indexPath) in
guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerId", for: indexPath) as? HeaderView else { return nil }
return header
}
}

@objc func onButtonClick() {
print("toggle")
showHeader.toggle()
collectionView.collectionViewLayout = createCompositionalLayout()
collectionView.layoutIfNeeded()
}

func addData() {
var snapshot = NSDiffableDataSourceSnapshot<Section, Int>()
var sections: [Section] = []
sections.append(Section(items: [0,1,2,3,4,5,6,7,8,9,10,11,12]))

snapshot.appendSections(sections)

for section in sections {
snapshot.appendItems(section.items, toSection: section)
}

dataSource?.apply(snapshot)
}

}

class CellView: UICollectionViewCell {
let button = UIButton()

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .red

addSubview(button)
button.setTitle("Toggle header", for: .normal)
button.setTitleColor(.black, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.topAnchor.constraint(equalTo: topAnchor).isActive = true
button.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class HeaderView: UICollectionReusableView {
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .blue
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

最佳答案

如果我答对了你的问题,你可以将 Collection View 滚动到顶部:

collectionView.setContentOffset(.zero, animated: false)

任何时候你想显示标题。这是一种解决方法,但我认为它应该可以解决问题。

关于ios - collectionview global header 不会将单元格向下推而是保持在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65422920/

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