UICollectionViewCompositionalLayout

Hello,

how should I perform two next two operations with UICompositionalLayout?

  1. Scroll To Specific offset
  2. Change layout of the item, I'm scrolling to.

Whenever I try to call collectionView.collectionViewLayout.invalidateLayout(), I get reset:

  1. Content Offset of UICollectionView
  2. The layout change of the item is not updated

Here is the example of the layout I try to use.

   let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),

                                          heightDimension: .estimated(100))

    let item = NSCollectionLayoutItem(layoutSize: itemSize)



    item.edgeSpacing = NSCollectionLayoutEdgeSpacing(leading: nil, top: .fixed(8), trailing: nil, bottom: .fixed(8))



    let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),

                                           heightDimension: .estimated(100))



    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])



    let section = NSCollectionLayoutSection(group: group)



    return section

And here is the example of cell

class ReelTextCellView: UICollectionViewCell {
  private var textView: UITextView!
  private var infoHolderView: UIView!
  private var copyButton: UIButton!
  private var scrollView: UIScrollView!
  private var heightConstraint: NSLayoutConstraint!

  var isExpanded = false



  override init(frame: CGRect) {
    super.init(frame: .zero)

    buildView()
  }

  required init?(coder: NSCoder) {

    fatalError("init(coder:) has not been implemented")

  }



  override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {

    let attributes = super.preferredLayoutAttributesFitting(layoutAttributes)



    attributes.frame.size = .init(width: contentView.frame.width, height: isExpanded ? 300 : 100)



    return attributes

  }

  // Called with the cellWillAppear and manually for indexPathsForVisibleItems
  func update(with appeareance: Appearance) {

    isExpanded = appeareance.isExpanded

    ///    heightConstraint.constant = appeareance.isExpanded ? 300 : 100

  }

private extension ReelTextCellView {



  func buildView() {

    infoHolderView = UIView()



    contentView.addSubview(infoHolderView) {

      contentView.bottomAnchor.constraint(equalTo: infoHolderView.bottomAnchor)

      contentView.leadingAnchor.constraint(equalTo: infoHolderView.leadingAnchor)

      contentView.trailingAnchor.constraint(equalTo: infoHolderView.trailingAnchor)

      infoHolderView.heightAnchor.constraint(equalToConstant: 30)

    }



    let separatorView = UIView()



    separatorView.backgroundColor = Assets.Colors.separator.color



    infoHolderView.addSubview(separatorView) {

      separatorView.topAnchor.constraint(equalTo: infoHolderView.topAnchor)

      separatorView.leadingAnchor.constraint(equalTo: infoHolderView.leadingAnchor)

      separatorView.trailingAnchor.constraint(equalTo: infoHolderView.trailingAnchor)

      separatorView.heightAnchor.constraint(equalToConstant: 0.5)

    }



    textView = UITextView()



    contentView.addSubview(textView) {

      textView.topAnchor.constraint(equalTo: contentView.topAnchor)

      textView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor)

      textView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)

      textView.bottomAnchor.constraint(equalTo: infoHolderView.topAnchor)

    }
  }
}

UICollectionViewCompositionalLayout
 
 
Q