I am trying to run a function when the keyboard shows and disappears and have the following code:
let notificationCenter = NotificationCenter.default
notific
class ViewController: UIViewController {
@IBOutlet weak var commentsTxt: UITextField!
@IBOutlet weak var keyboardBottom: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
IQKeyboardManager.shared.enable = false
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboarddidHide),
name: UIResponder.keyboardWillHideNotification,
object: nil
)
}
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.keyboardBottom.constant = keyboardHeight - self.bottomLayoutGuide.length
DispatchQueue.main.asyncAfter(deadline: .now()+0.1, execute: {
let bottomOffset = CGPoint(x: 0, y: self.scrlView.contentSize.height - self.scrlView.bounds.size.height)
self.scrlView.setContentOffset(bottomOffset, animated: true)
})
}
}
@objc func keyboarddidHide(_ notification: Notification) {
self.keyboardBottom.constant = 0
}
override func viewWillDisappear(_ animated: Bool) {
IQKeyboardManager.shared.enable = true
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
}
Swift 4.2
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveKeyboardNotificationObserver(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveKeyboardNotificationObserver(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
@objc func didReceiveKeyboardNotificationObserver(_ notification: Notification) {
let userInfo = notification.userInfo
let keyboardBounds = (userInfo!["UIKeyboardBoundsUserInfoKey"] as! NSValue).cgRectValue
let keyboardFrame = (userInfo!["UIKeyboardFrameEndUserInfoKey"] as! NSValue).cgRectValue
let duration = userInfo!["UIKeyboardAnimationDurationUserInfoKey"] as! Double
let curve = userInfo!["UIKeyboardAnimationCurveUserInfoKey"] as! Int
let frameBegin = (userInfo!["UIKeyboardFrameBeginUserInfoKey"] as! NSValue).cgRectValue
let centerBegin = (userInfo!["UIKeyboardCenterBeginUserInfoKey"] as! NSValue).cgPointValue
let center = (userInfo!["UIKeyboardCenterEndUserInfoKey"] as! NSValue).cgPointValue
let location = userInfo!["UIKeyboardIsLocalUserInfoKey"] as! Int
println("keyboardBounds: \(keyboardBounds) \nkeyboardFrame: \(keyboardFrame) \nduration: \(duration) \ncurve: \(curve) \nframeBegin:\(frameBegin) \ncenterBegin:\(centerBegin)\ncenter:\(center)\nlocation:\(location)")
switch notification.name {
case UIResponder.keyboardWillShowNotification:
// keyboardWillShowNotification
case UIResponder.keyboardWillHideNotification:
// keyboardWillHideNotification
default:
break
}
}
set keyboard notification observer in
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardNotification(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
}
and in your function handle it
func keyboardNotification(notification: NSNotification) {
print("keyboard displayed!!")
}
hope this will help you.