ios – Problems When Setting Up a UICollectionView Bottom Anchor When Displaying the Keyboard

So, my code is essentially a UIViewController with a UICollectionView that I've added inside (adding to its sub-view).

My problem is that the lower anchor constraint (for UICollectionView) will not change for my handleKeyboardWillHide function.
When you open the page and the keyboard, it opens and forces the anchor down to -300 (or whatever the keyboard height).
When I close the keyboard, the function calls, but the lower anchor constraint does not change.
It worked very well when I used UICollectionViewController, but for many other reasons, I had to switch to UIViewController. This error is now a source of error.

This is my code:

                collectionview = UICollectionView (frame: self.view.frame, collectionViewLayout: layout)
collectionview.dataSource = self
collectionview.delegate = self
collectionview.backgroundColor = .clear

collectionview.register (TextCell.self, forCellWithReuseIdentifier: cellId)
collectionview.register (UICollectionViewCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
collectionview.bounces = true
collectionview.alwaysBounceVertical = true // Scroll
collectionview.keyboardDismissMode = UIScrollView.KeyboardDismissMode.interactive // ​​Delete keyboard while scrolling

collectionview.contentInset = UIEdgeInsets (top: 0, left: 0, bottom: 8, right: 0)
collectionview.translatesAutoresizingMaskIntoConstraints = false
collectionview.leftAnchor.constraint (equalTo: view.leftAnchor) .isActive = true
collectionview.topAnchor.constraint (equalTo: view.topAnchor, constant: 20) .isActive = true
collectionview.widthAnchor.constraint (equalTo: view.widthAnchor) .isActive = true

self.view.addSubview (collectionview)

NotificationCenter.default.addObserver (self, selector: #selector (handleKeyboardWillShow), name: UIResponder.keyboardWillShowNotification, subject: nil)

NotificationCenter.default.addObserver (self, selector: #selector (handleKeyboardWillHide), name: UIResponder.keyboardWillHideNotification, subject: nil)

}

@objc func handleKeyboardWillShow (notification: NSNotification) {
let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect
let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] like AnyObject) .doubleValue

collectionview.bottomAnchor.constraint (equalTo: self.view.bottomAnchor, constant: -300) .isActive = true

print (" n will display:", -keyboardFrame! .height)
UIView.animate (withDuration: keyboardDuration!) {
self.view.layoutIfNeeded ()
}
}

@objc func handleKeyboardWillHide (notification: NSNotification) {
let keyboardDuration = (notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] like AnyObject) .doubleValue

collectionview.bottomAnchor.constraint (equalTo: self.view.bottomAnchor, constant: -100) .isActive = true

print (" n hide: -100")
UIView.animate (withDuration: keyboardDuration!) {
self.view.layoutIfNeeded ()
}

// move the input field one way or the other ..
}

It's been a long time since I got stuck on it and I'm not sure what I'm doing wrong. Thank you!