Hanlde your view when keyboard show or hide, make sure that your input view is visible to user.
Simple, easy to use.
Step 1: pod repo update
if need
Step 2: Add to Podfile
pod 'KeyboardShowHideHandler'
Step 3: pod install
Make your view controller conform KeyboardHandler
, at viewWillAppear
/viewWillDisappear
just call addObservingKeyboard
/removeObservingKeyboard
to register/unregister the listener.
class ViewController: UIViewController {
@IBOutlet var scrollView: UIScrollView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addObservingKeyboard()
}
override func viewWillDisappear(_ animated: Bool) {
removeObservingKeyboard()
}
}
extension ViewController: KeyboardHandler {
var contentScrollView: UIScrollView {
scrollView
}
}
extension ViewController: KeyboardHandler {
var contentScrollView: UIScrollView {
scrollView
}
var distanceToKeyboard: Int {
return 10 /// distance from target input to top of keyboard
}
var tapAnywhereToDismissKeyboard: Bool {
return true /// tap outside target view to dismiss keyboard
}
var supportViewTypes: [UIView.Type] {
/// Which views type to apply for
return [
UITextField.self, UITextView.self
]
}
}