调出键盘时自动更改滚动插图的动画

时间:2018-12-23 21:46:51

标签: swift xcode uicollectionview autolayout interface-builder

更改滚动视图的插图时,它们会不希望地与键盘的显示和隐藏动画化

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

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
}


}

我希望滚动视图可以立即更改大小,但是实际的更改是逐步的,并使用键盘进行了动画处理。我怀疑这与在与键盘显示相关的某种隐藏方法中更新布局有关,但这会影响其余视图。

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

2 个答案:

答案 0 :(得分:1)

您正在键盘动画中执行布局(因为通知是在此动画期间发送的),因此可动画化的更改(例如,您对后续显式布局遍历中的插图和帧更改的操作)会在其中动画化那个动画。如果您确实需要在没有动画的情况下执行这些操作,则可以使用[UIView performWithoutAnimation:]

答案 1 :(得分:0)

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

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