Manage Keyboard And TextField iOS

SOURCE

// Created by Kapil Ahuja on 6/16/17.
// Copyright © 2017 Kapil Ahuja. All rights reserved.
//

import UIKit

/* Class Contain all Keyboard management related methods */

final class KeyboardManager: NSObject {

//MARK: – Instance
static let shared = KeyboardManager()
lazy var textFieldDetails : (textField : UITextField, maxY : CGFloat, isAnimated : Bool, viewY : CGFloat)! = (UITextField(), 0, false, 0)

/* Register For KeyBoard & TextField Notification */
func RegisterForNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(textFieldViewDidBeginEditing), name: Notification.Name.UITextFieldTextDidBeginEditing, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: Notification.Name.UIKeyboardWillHide, object: nil)
}

/* UnRegister For KeyBoard & TextField Notification */
func UnRegisterForNotifications() {
NotificationCenter.default.removeObserver(self, name: Notification.Name.UITextFieldTextDidBeginEditing, object: nil)
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)
}

/* UITextFieldTextDidBeginEditing Notification */
func textFieldViewDidBeginEditing(_ notification:Notification) {

// Getting object & Saving in Tuple
if let textField = notification.object as? UITextField {
let tectFieldPoints = textField.convert((textField.frame.origin), to: UIScreen.main.coordinateSpace)
textFieldDetails = (textField, tectFieldPoints.y + textField.frame.size.height, false, 0)
}

}

/* UIKeyboardWillShow Notification */
func keyboardWillShow(_ notification:Notification) {

if let info = notification.userInfo {

// Getting UIKeyboardSize.
if let kbFrame = (info[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

let screenSize = UIScreen.main.bounds

//Calculating actual keyboard displayed size, keyboard frame may be different when hardware keyboard is attached

//Check if Text Field is Active
guard textFieldDetails != nil else {
return
}

//Get Size and Check for Animation
let intersectRect = kbFrame.intersection(screenSize)
if intersectRect.origin.y < textFieldDetails.maxY {

if let controller = textFieldDetails.textField.parentViewController {

//Set Values of Animation
self.textFieldDetails.viewY = controller.view.frame.origin.y
self.textFieldDetails.isAnimated = true

//Animate Window
UIView.animate(withDuration: 0.5, animations: {
controller.view.frame = CGRect(x: controller.view.frame.origin.x, y: intersectRect.origin.y – self.textFieldDetails.maxY, width: controller.view.frame.size.width, height: controller.view.frame.size.height)

}, completion: nil)
}
}

}
}
}

/* UIKeyboardWillHide Notification */
func keyboardWillHide(_ notification:Notification) {

//Check if animated text field
if textFieldDetails.isAnimated {

if let controller = textFieldDetails.textField.parentViewController {

//Animate Window to reset
UIView.animate(withDuration: 0.5, animations: {
controller.view.frame = CGRect(x: controller.view.frame.origin.x, y: self.textFieldDetails.viewY, width: controller.view.frame.size.width, height: controller.view.frame.size.height)
}, completion: nil)
}
}
}
}

//MARK: – UIView Extension For Parent View Controller
extension UIView {

//Fetch Parent View Controller From UIView
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}

IMAGES

USE

/* Register For Notification */
KeyboardManager.shared.RegisterForNotifications()

Manage Keyboard And TextField iOS