将徽章(补充视图)添加到 UICollectionLayoutListConfiguration

pab*_*ros 5 uikit ios uicollectionview uicollectionviewlayout swift

有什么方法可以将徽章NSCollectionLayoutSupplementaryItem) 添加到 a吗UICollectionLayoutListConfiguration

我正在尝试使用现代集合视图实现侧边栏:

var configuration = UICollectionLayoutListConfiguration(appearance: .sidebar)
...

let section = NSCollectionLayoutSection.list(using: configuration, layoutEnvironment: layoutEnvironment)
Run Code Online (Sandbox Code Playgroud)

但我找不到如何实现badge配置:

var configuration = UICollectionLayoutListConfiguration(appearance: .sidebar)
...

let section = NSCollectionLayoutSection.list(using: configuration, layoutEnvironment: layoutEnvironment)
Run Code Online (Sandbox Code Playgroud)

就像Apple提供的示例代码一样:

let badgeAnchor = NSCollectionLayoutAnchor(edges: [.top, .trailing], fractionalOffset: CGPoint(x: 0.3, y: -0.3))
let badgeSize = NSCollectionLayoutSize(widthDimension: .absolute(20),
                                                  heightDimension: .absolute(20))
let badge = NSCollectionLayoutSupplementaryItem(
                layoutSize: badgeSize,
                elementKind: "badge",
                containerAnchor: badgeAnchor)
Run Code Online (Sandbox Code Playgroud)

(苹果提供的示例代码,顺便说一句崩溃了)

关于如何实现徽章UICollectionLayoutListConfiguration或不可能的任何想法?

小智 3

在示例项目中,element kind forbadge是错误的。它是

let badge = NSCollectionLayoutSupplementaryItem(
            layoutSize: badgeSize,
            elementKind: ItemBadgeSupplementaryViewController.badgeElementKind,
            containerAnchor: badgeAnchor)
Run Code Online (Sandbox Code Playgroud)

错误消息实际上是有道理的:

"the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath: does not match the element kind it is being used for. When asked for a view of element kind 'badge-element-kind' the data source dequeued a view registered for the element kind 'badge-reuse-identifier'."
Run Code Online (Sandbox Code Playgroud)

只需elementKind将徽章 ( 中ItemBadgeSupplementaryViewController) 更改为

elementKind: BadgeSupplementaryView.reuseIdentifier
Run Code Online (Sandbox Code Playgroud)

为了回答您的问题,徽章将是您的补充项目NSCollectionLayoutItem