6

I am building a page in my app that is very similar to the messages app. This page has a toolbar at the top, a collection view directly under that, and a view under the collection view that contains a text field for sending a message.

The collection view is a list of messages between two users. It is comprised of self-sizing cells using auto-layout.

I have added a keyboardNotification listener as suggested by many in order to move my text field up when the keyboard is introduced onto the screen. In order for this to work, the view containing the text field has a constraint that keeps it to the bottom layout guide. When the keyboard is brought up the constant on that constraint is changed to adapt to the keyboard height.

This works well, but where my problem lies is in the fact that in order to animate a constraint change, one must call view.layoutIfNeeded(). This causes my collection view cells to resize briefly and animate. It is very annoying and looks terrible, so I am asking: how do I keep the collection view cells from animating when layoutIfNeeded is called on the parent view?

Justin Waite
  • 835
  • 2
  • 8
  • 16

1 Answers1

5

Try this:

// ... modification on constraints
UIView.animate(withDuration: 0.4) {
    // To lock the animation for collectionView.
    UIView.performWithoutAnimation {
        self.collectionView.performBatchUpdates(nil, completion: nil)
    }
    self.view.layoutIfNeeded()
}
Henry H Miao
  • 3,420
  • 4
  • 20
  • 26