swift - 如何使用 UICollectionViewCompositionalLayout 创建像图像这样的 Collection View

标签 swift collections view collectionview uicollectionviewcompositionallayout

有几天我无法弄清楚如何使用 UICollectionViewCompositionalLayout 制作像屏幕截图中那样的 Collection View 。谁能帮忙? Image!!!!

最佳答案

您可以在水平组中组合两个垂直 NSCollectionLayoutGroup 来实现此布局。每个垂直组由两个 itens 组成,一大一小。

final class CustomLayout {



class func create() -> UICollectionViewCompositionalLayout{
    
    //Obs: The size of each element is relative to his parent.
    //NSCollectionLayoutItem < NSCollectionLayoutGroup < NSCollectionLayoutSection < UICollectionViewCompositionalLayout
    
    
    
    //Defines the margin
    let margin: CGFloat = 5
    //Defines the space between the cells
    let contentInsests: CGFloat = 8

    
    // Create the big item that is 60% of your parent's height
    let bigItem = NSCollectionLayoutItem(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .fractionalHeight(0.60)))
    
    bigItem.contentInsets = NSDirectionalEdgeInsets(
        top: contentInsests,
        leading: contentInsests,
        bottom: contentInsests,
        trailing: contentInsests)
    
    // Create the small item that is 40% of your parent's height
    let smallItem = NSCollectionLayoutItem(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .fractionalHeight(0.40)))
    
    smallItem.contentInsets = NSDirectionalEdgeInsets(
        top: contentInsests,
        leading: contentInsests,
        bottom: contentInsests,
        trailing: contentInsests)
    

    //Here we Combine the two itens in a vertical group
    let group1 = NSCollectionLayoutGroup.vertical(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(0.5),
            heightDimension: .fractionalHeight(1.0)),
        subitems: [bigItem,smallItem])
    
    //Here we do the same reversing the order of the itens
    let group1Reversed = NSCollectionLayoutGroup.vertical(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(0.5),
            heightDimension: .fractionalHeight(1.0)),
        subitems: [smallItem, bigItem])
    
    
    //here we combine both in a nested group making the layout like in the image.
    //In this example we are making a custom layout with two "Image layout" per screen, so
   // We define his height as 0.5% of his parent's height. You can change it for your preference.
    let nestedGroup = NSCollectionLayoutGroup.horizontal(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .fractionalHeight(0.5)),
        subitems: [
          group1,
        group1Reversed
        ]
    )
    
    
    //Here is our main group with two nested group
    let mainGroup = NSCollectionLayoutGroup.vertical(
        layoutSize: NSCollectionLayoutSize(
            widthDimension: .fractionalWidth(1.0),
            heightDimension: .fractionalHeight(1.0)),
        subitems: [
          nestedGroup,nestedGroup
        ]
    )
    
    
    
    let section = NSCollectionLayoutSection(group: mainGroup)
    
    
    section.contentInsets = NSDirectionalEdgeInsets(
        top: 0,
        leading: margin,
        bottom: 0,
        trailing: margin)
    
    let configuration = UICollectionViewCompositionalLayoutConfiguration()
    configuration.scrollDirection = .vertical
    let layout = UICollectionViewCompositionalLayout(section: section)
    layout.configuration = configuration
    return layout
}}

Example

关于swift - 如何使用 UICollectionViewCompositionalLayout 创建像图像这样的 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64581542/

相关文章:

MySQL - 在 VIEW 选择上添加 'group by' 规则

android-gallery 如何设置被选中的 child 的位置

swift - 每 X 天本地通知 - Swift

ios - 更改 UILabel 文本会导致可移动的 UIImageView 对象恢复到原始位置

swift - 如何从 SwiftyJSON 创建对象

java - 从 map 存储条目是否安全?它会导致内存泄漏吗?

c# - 将嵌套字典转换为 IReadOnlyDictionary

swift - Realm 对象的自定义 setter/willSet

java - 如何在不使用任何集合和循环的情况下删除 List<MyObject> 中的重复对象

mysql - MySQL中用 View 替换表