We may need keyboard infomation from keyboard notification to do animation. However, the approach is complicated and easy to make mistakes.
But KeyboardMan will make it simple & easy.
Swift 2.3, iOS 8.0
import KeyboardMan
Do animation with keyboard appear/disappear:
let keyboardMan = KeyboardMan()
keyboardMan.animateWhenKeyboardAppear = { [weak self] appearPostIndex, keyboardHeight, keyboardHeightIncrement in
print("appear \(appearPostIndex), \(keyboardHeight), \(keyboardHeightIncrement)\n")
if let strongSelf = self {
strongSelf.tableView.contentOffset.y += keyboardHeightIncrement
strongSelf.tableView.contentInset.bottom = keyboardHeight + strongSelf.toolBar.frame.height
strongSelf.toolBarBottomConstraint.constant = keyboardHeight
strongSelf.view.layoutIfNeeded()
}
}
keyboardMan.animateWhenKeyboardDisappear = { [weak self] keyboardHeight in
print("disappear \(keyboardHeight)\n")
if let strongSelf = self {
strongSelf.tableView.contentOffset.y -= keyboardHeight
strongSelf.tableView.contentInset.bottom = strongSelf.toolBar.frame.height
strongSelf.toolBarBottomConstraint.constant = 0
strongSelf.view.layoutIfNeeded()
}
}
For more specific information, you can use keyboardInfo that KeyboardMan post:
keyboardMan.postKeyboardInfo = { [weak self] keyboardMan, keyboardInfo in
// TODO
}
Check the demo for more information.
另有中文介绍。
Feel free to drag KeyboardMan.swift
to your iOS Project. But it's recommended to use CocoaPods or Carthage.
pod 'KeyboardMan', '~> 0.7.0'
github "nixzhu/KeyboardMan" >= 0.7.0
NIX @nixzhu
KeyboardMan is available under the MIT license. See the LICENSE file for more info.