调出键盘时,更改滚动插图会自动设置动画

Changing scroll insets is automatically animated when bringing up the keyboard

当更改滚动视图的插图时,它们会随着键盘的显示和隐藏而出现令人不快的动画效果

这是我应用程序的消息传递部分。当我想调出键盘时,我需要立即更改显示消息的集合视图的滚动视图的大小

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource, 
UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

@IBOutlet weak var myCollectionView: UICollectionView!
@IBOutlet weak var textfield: UITextField!


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 20
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    cell.backgroundColor = UIColor.random()
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.bounds.width, height: 50)
}

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidHide), name: UIResponder.keyboardDidHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)


}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

}

@objc func handleKeyboardDidHide(notification: NSNotification){
    //we only wanna do this if we're at the bottom
    //      if  checkIfScrolledToPosition(distanceFromBottom: 0)    {
    //
    //          self.scrollToBottomOfCollectionView(animated: true)
    //      }
}

@objc func handleKeyboardDidShow(notification: NSNotification){
    //temp commented for testing
    //      if shouldScrollToBottomOnceKeyboardShow{
    //          self.scrollToBottomOfCollectionView(animated: true)
    //          shouldScrollToBottomOnceKeyboardShow = false
    //      }
}
@objc func handleKeyboardNotification(notification: NSNotification?){

    if let keyboardFrame: NSValue = notification?.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
        let isKeyboardShowing = notification!.name == UIResponder.keyboardWillShowNotification

        if isKeyboardShowing{
            setCollectionViewScrollInsets(heightFromBottom: keyboardHeight)
        }else{
            setCollectionViewScrollInsets(heightFromBottom: 0)

        }
    }

}


@IBAction func button(_ sender: Any) {
    textfield.resignFirstResponder()
}

func setCollectionViewScrollInsets(heightFromBottom: CGFloat){
    //  messagesCollectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: heightFromBottom, right: 0)
    myCollectionView.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: heightFromBottom, right: 0)
    //myCollectionView.layoutIfNeeded()//without this is seems to cause severe fuck up
}


}

我希望滚动视图能够立即改变大小,但实际的改变是渐进的,并且是通过键盘进行动画处理的。我怀疑这是在一些与键盘显示相关的隐藏方法中更新布局,但这会影响视图的其余部分。

这个问题的一个可能的解决方案是在键盘显示时强制滚动视图保持滚动到底部,这样我就不必担心插图会立即改变,如果有人知道该怎么做的话那。

如果您使用的是自动布局,请获取 collectionView 底部约束的出口,然后当您需要更新时 UI,将该约束的常量设置为键盘高度的值。之后调用 layoutIFNeeded,并更新约束:

collectionViewBottomConstraint.constant = keyboardHeight // you need to create this constraint as an outlet.
collectionView.layoutIfNeeded()
collectionView.updateConstraints()

您正在键盘动画中执行您的布局(因为通知是在此动画期间发送的),因此您的动画更改(例如,您对后续显式布局过程中的插图和框架更改的操作)是在该动画中进行动画处理。如果你真的需要在没有动画的情况下执行这些操作,你可以使用 [UIView performWithoutAnimation:]