0

I am using a UICollectionView for chat and using scroll to bottom on load. It's all working good except the first time I scroll manually it bounces up about 50-100 pt. If I continue scrolling it's fine and goes into the safe area and below the screen.

What is causing the initial bounce? It seems that it bounces off the top of the bottom safe area first, and then goes below the safe area fine if you continue scrolling.

What I'm trying to get is a nice smooth scroll initially without the weird bounce.

It seems to be caused by the fact that I am dynamically sizing image heights after they load with invalidateLayout.

Timmerz
  • 5,746
  • 5
  • 29
  • 44

1 Answers1

0

I was able to solve this by luck with the following

AutoSizing cells: cell width equal to the CollectionView

// UICollectionViewController

if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
    layout.estimatedItemSize = CGSize(width: view.frame.width, height: 100)
}
        
// UICollectionViewCell

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    let autoLayoutAttributes = super.preferredLayoutAttributesFitting(layoutAttributes)
    
    // Specify you want _full width_
    let targetSize = CGSize(width: layoutAttributes.frame.width, height: 200)
    
    // Calculate the size (height) using Auto Layout
    let autoLayoutSize = contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.required, verticalFittingPriority: UILayoutPriority.defaultLow)
    let autoLayoutFrame = CGRect(origin: autoLayoutAttributes.frame.origin, size: autoLayoutSize)
    
    // Assign the new size to the layout attributes
    autoLayoutAttributes.frame = autoLayoutFrame
    return autoLayoutAttributes
}
Timmerz
  • 5,746
  • 5
  • 29
  • 44