gpt4 book ai didi

ios - 可以使用 UICollectionViewCompositionalLayout 制作此布局

转载 作者:行者123 更新时间:2023-12-01 15:44:13 27 4
gpt4 key购买 nike

我正在尝试使用 UICollectionViewCompositionalLayout 创建一个“标签云”部分
我想要一个全宽的部分,然后是一个以这种方式包裹的部分
example
必备 人力资源 , 财务健康 员工敬业度 每个都是一个部分内的一个单元格。请忽略其他 UI 元素。
此部分还需要支持多行,因为可能有 1 到 15 个标签。
我已经尝试过,但无法使用 intrinsicContentSize 来创建这种包装效果,也没有每个项目的宽度每个项目的。
enter image description here
到目前为止,我目前的尝试是

import UIKit

final class CustomCell: UICollectionViewCell {
let label = UILabel(frame: .zero)

override init(frame: CGRect) {
super.init(frame: frame)
label.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(label)
NSLayoutConstraint.activate([
label.topAnchor.constraint(equalTo: contentView.topAnchor),
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
])
}

required init?(coder: NSCoder) {
return nil
}

}


protocol SectionData {
var text: String { get }
}

struct DummyData: SectionData {
let text: String
}

enum SectionType: Int, CaseIterable {
case single
case double
case carousel
case tags
}

struct Section {
let id: Int
let type: SectionType
let title: String?
let subtitle: String?
let data: [SectionData]
}

class ViewController: UIViewController {

private var items: [Section] = [] {
didSet { collectionView.reloadData() }
}

private(set) lazy var collectionView: UICollectionView = {
let collectionView = UICollectionView(frame: view.frame, collectionViewLayout: makeLayout())
collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
collectionView.backgroundColor = .systemBackground
collectionView.dataSource = self
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: "CustomCell")
return collectionView
}()

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)

items = [
Section(id: 0, type: .single, title: nil, subtitle: nil, data: Array(0...3).map { index in DummyData(text: "Item \(index)") }),
Section(id: 1, type: .tags, title: nil, subtitle: nil, data: Array(0...15).map { index in DummyData(text: "Item \(index)") })
]

}
}

extension ViewController: UICollectionViewDataSource {

func numberOfSections(in collectionView: UICollectionView) -> Int {
return items.count
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items[section].data.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let model = items[indexPath.section].data[indexPath.item]
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.backgroundColor = indexPath.item % 2 == 0 ? .darkGray : .lightGray
cell.label.text = model.text
return cell
}

}


extension ViewController {

func makeLayout() -> UICollectionViewLayout {
let layout = UICollectionViewCompositionalLayout { [weak self] index, env in
guard let self = self else { return nil }
let section = self.items[index]
switch section.type {
case .single: return self.makeSingleSection()
case .tags: return self.makeTagSection(count: section.data.count)
default: return nil
}
}

let config = UICollectionViewCompositionalLayoutConfiguration()
config.interSectionSpacing = 20
layout.configuration = config
return layout
}

func makeSingleSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(100))
let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize, subitems: [layoutItem])
let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
return layoutSection
}

func makeTagSection(count: Int) -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1))
let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)
let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(100))
let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize, subitem: layoutItem, count: count)
let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
return layoutSection
}
}

我真的很感激我能帮助我理解如何实现这种布局。

最佳答案

您可以创建该部分布局,但是您的工厂方法中存在一些错误。
像这样的东西会产生我相信你想要的效果 -

  func makeTagSection(count: Int) -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .estimated(100), heightDimension: .absolute(32))
let layoutItem = NSCollectionLayoutItem(layoutSize: itemSize)

let layoutGroupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: itemSize.heightDimension)
let layoutGroup = NSCollectionLayoutGroup.horizontal(layoutSize: layoutGroupSize, subitems: [layoutItem])

layoutGroup.interItemSpacing = .fixed(8)

let layoutSection = NSCollectionLayoutSection(group: layoutGroup)
layoutSection.contentInsets = .init(top: 0, leading: 16, bottom: 0, trailing: 16)
layoutSection.interGroupSpacing = 8
return layoutSection
}
enter image description here

关于ios - 可以使用 UICollectionViewCompositionalLayout 制作此布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63092487/

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