【问题标题】:How to style a UICollectionViewCompositionalLayout header to be separated from its children list .insetGrouped layout?如何设置 UICollectionViewCompositionalLayout 标头的样式以与其子列表 .insetGrouped 布局分开?
【发布时间】:2021-11-28 18:30:20
【问题描述】:

我试图模仿我们在邮件应用程序中发现的外观,其中邮箱的组标题是可折叠的,并且其子列表的样式使用 .insetGrouped layoutOption。

我尝试使用 UICollectionLayoutListConfiguration(appearance: .sidebarPlain) 作为配置,但我没有找到一种方法来设置部分的 .insetGrouped 外观样式

邮件应用:

我最接近这个布局的是:

我正在使用下面的方法来配置我的 ViewController 中的 collectionView。

let mixedListLayout = 
    UICollectionViewCompositionalLayout { section, env -> NSCollectionLayoutSection? in
    
    let listConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)

    return NSCollectionLayoutSection.list(using: listConfig, layoutEnvironment: env)
}

collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: mixedListLayout)
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]       
collectionView.delegate = self

view.addSubview(collectionView)

对于单元格注册,我的方法是这样的:

let headerRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, SidebarItem> { (cell, _, item) in

    var contentConfiguration = UIListContentConfiguration.sidebarHeader()
    contentConfiguration.text = item.title
    cell.contentConfiguration = contentConfiguration
    let outlineDisclosureConfig = UICellAccessory.OutlineDisclosureOptions(style: .header)
    cell.accessories = [.outlineDisclosure(displayed: .always, options: outlineDisclosureConfig)]
}

let primaryRowRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, SidebarItem> { (cell, _, item) in

    var contentConfiguration = UIListContentConfiguration.sidebarCell()
    contentConfiguration.text = item.title
    contentConfiguration.image = item.image

    cell.contentConfiguration = contentConfiguration
    cell.indentationLevel = 0
    if let acessoryLabel = item.subtitle {
        cell.accessories = [.label(text: acessoryLabel)]
    }
    cell.accessories.append(.disclosureIndicator())
}

dataSource = UICollectionViewDiffableDataSource<SidebarSection, SidebarItem>(collectionView: collectionView) {
    (collectionView, indexPath, item) -> UICollectionViewCell in

    switch item.type {
    case .header:
        return collectionView.dequeueConfiguredReusableCell(using: headerRegistration, for: indexPath, item: item)
    case .primaryRow:
        return collectionView.dequeueConfiguredReusableCell(using: primaryRowRegistration, for: indexPath, item: item)
    }
}

【问题讨论】:

    标签: ios uikit uicollectionviewcell sidebar uicollectionviewcompositionallayout


    【解决方案1】:

    在阅读了更多文档后,我通过在UICollectionLayoutListConfiguration 中将headerMode 设置为.firstItemInSection 解决了这个问题,所以我的mixedListLayout 如下所示:

    let mixedListLayout = 
        UICollectionViewCompositionalLayout { section, env -> NSCollectionLayoutSection? in
        
        var listConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
        
        listConfig.headerMode = section == 0 ? .none : .firstItemInSection // Using "first
    
        return NSCollectionLayoutSection.list(using: listConfig, layoutEnvironment: env)
    }
    
    

    【讨论】:

      【解决方案2】:

      我认为您应该将outline groupdisclosure group 结合使用

      【讨论】:

      • 感谢您的回复,但不幸的是,我没有在此组件上使用 swiftUI,并且拥有 UIHostingController 会给布局增加不必要的复杂性
      猜你喜欢
      • 2016-08-09
      • 2012-08-20
      • 2021-02-05
      • 2020-10-30
      • 2011-05-24
      • 1970-01-01
      • 2023-03-18
      • 2020-10-22
      • 2020-04-21
      相关资源
      最近更新 更多