UICollectionView not scrolling to bottom on the first few items
<p><a href="https://i.stack.imgur.com/PqCY0.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/PqCY0.gif" alt=""></a></p>

<p>I've been trying to create a chat interface that I can reuse. I'm almost done with the implementation, but there's something that keeps bugging me about it. If I start loading in messages like in the gif when I first load the interface you can see that after the 4th message there are 3 messages that don't scroll to the bottom. With the 8th being the first one that does finally scroll. This varies according to the screen size. On the iPhone 6s testing device it reaches the 9th message being the one that scrolls.</p>

<p>I'm using content inset as the method to keep the collectionview visible with the following code being run every time the frame of the UIToolbar at the bottom changes</p>

<pre><code>toolBar.inputAccessoryViewFrameChanged = {(rect: CGRect) in Void()
    let navigationAndStatusHeight = self.navigationController != nil &amp;&amp; self.navigationController!.navigationBar.isTranslucent ? self.navigationController!.navigationBar.frame.size.height + UIApplication.shared.statusBarFrame.height : 0
    self.collectionView.contentInset = UIEdgeInsets(top: navigationAndStatusHeight + 8, left: 8, bottom: UIScreen.main.bounds.height - rect.origin.y + 8, right: 8)
    self.collectionView.scrollIndicatorInsets.bottom = UIScreen.main.bounds.height - rect.origin.y
}
</code></pre>

<p>This code is run every time a new message is inserted:</p>

<pre><code>func insertNewMessage(){
    self.collectionView.performBatchUpdates({
        self.collectionView.insertItems(at: [NSIndexPath(item: self.numberOfMessages() - 1, section: 0) as IndexPath])
        }) { (Bool) in
        self.scrollToBottom(animated: true)
    }
}
</code></pre>

<p>with the scrollToBottom function being:</p>

<pre><code>func scrollToBottom(animated: Bool){
    guard self.numberOfMessages() &gt; 0 else{
        return
    }
    self.collectionView.scrollToItem(at: IndexPath(item: self.numberOfMessages() - 1, section: 0), at: UICollectionViewScrollPosition.top , animated: animated)  
}
</code></pre>

<p>I'm currently running on this version of XCode Version 8.1 beta (8T29o) &amp; iOS 10.1(14B55c)</p>