The Solution I found is
private func createGridLayout() -> UICollectionViewCompositionalLayout {
// MARK: - Define the Item Layouts
// Define layout for top cells (1/3 width)
let topCellItemLayout = CompositionalLayout.createItemLayout(width: .fractionalWidth(1/3), height: .fractionalHeight(1.0), specing: 1)
// Define layout for second cells (2/3 width)
let secondCellItemLayout = CompositionalLayout.createItemLayout(width: .fractionalWidth(2/3), height: .fractionalHeight(1.0), specing: 1)
// Define layout for single cells (full width)
let singleCellItemLayout = CompositionalLayout.createItemLayout(width: .fractionalWidth(1.0), height: .fractionalHeight(1.0), specing: 1)
// MARK: - Apply Content Insets
// Apply content insets to top cell item layout
topCellItemLayout.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)
// Apply content insets to second cell item layout
secondCellItemLayout.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)
// Apply content insets to single cell item layout
singleCellItemLayout.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5)
// MARK: - Create Groups
// Create group layout for top cells
let topCellGroupLayout = CompositionalLayout.createGroupLayout(alignment: .horizontal, width: .fractionalWidth(1.0), height: .fractionalWidth(4/9), item: topCellItemLayout, count: 3)
// Create group layout for single cells
let singleCellGroupLayout = CompositionalLayout.createGroupLayout(alignment: .vertical, width: .fractionalWidth(1/3), height: .fractionalHeight(1.0), items: [singleCellItemLayout])
// Create group layout for second cells and single cell group
let secondCellGroupLayout = CompositionalLayout.createGroupLayout(alignment: .horizontal, width: .fractionalWidth(1.0), height: .fractionalWidth(4/9), items: [secondCellItemLayout, singleCellGroupLayout])
// Create group layout for container with second cell and single cell groups
let containerGroupsLayout = CompositionalLayout.createGroupLayout(alignment: .horizontal, width: .fractionalWidth(1.0), height: .fractionalWidth(4/9), items: [secondCellItemLayout, singleCellGroupLayout])
// Create group layout for fourth container with single cell and second cell groups
let fourthContainerGroupLayout = CompositionalLayout.createGroupLayout(alignment: .horizontal, width: .fractionalWidth(1.0), height: .fractionalWidth(4/9), items: [singleCellGroupLayout, secondCellItemLayout])
// Create main container group layout with all the above group layouts
let mainContainerGroupLayout = CompositionalLayout.createGroupLayout(alignment: .vertical, width: .fractionalWidth(1.0), height: .fractionalWidth(16/9), items: [topCellGroupLayout, secondCellGroupLayout, topCellGroupLayout, fourthContainerGroupLayout])
// MARK: - Create Section
// Create the section layout with the main container group layout
let sectionLayout = NSCollectionLayoutSection(group: mainContainerGroupLayout)
// MARK: - Return the Layout
// Return the compositional layout with the section layout
return UICollectionViewCompositionalLayout(section: sectionLayout)
}
}
enum CompositionalGroupAlignment {
case vertical
case horizontal
}
struct CompositionalLayout {
static func createItemLayout(width: NSCollectionLayoutDimension, height: NSCollectionLayoutDimension, specing: CGFloat) -> NSCollectionLayoutItem {
let item = NSCollectionLayoutItem(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height))
item.contentInsets = NSDirectionalEdgeInsets(top: specing, leading: specing, bottom: specing, trailing: specing)
return item
}
static func createGroupLayout(alignment: CompositionalGroupAlignment, width: NSCollectionLayoutDimension, height: NSCollectionLayoutDimension, items: [NSCollectionLayoutItem]) -> NSCollectionLayoutGroup {
switch alignment {
case .vertical:
return NSCollectionLayoutGroup.vertical(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), subitems: items)
case .horizontal:
return NSCollectionLayoutGroup.horizontal(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), subitems: items)
}
}
static func createGroupLayout(alignment: CompositionalGroupAlignment, width: NSCollectionLayoutDimension, height: NSCollectionLayoutDimension, item: NSCollectionLayoutItem, count: Int) -> NSCollectionLayoutGroup {
switch alignment {
case .vertical:
return NSCollectionLayoutGroup.vertical(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), repeatingSubitem: item, count: count)
case .horizontal:
return NSCollectionLayoutGroup.horizontal(layoutSize: NSCollectionLayoutSize(widthDimension: width, heightDimension: height), repeatingSubitem: item, count: count)
}
}
}