class ViewController: UIViewController {
@IBOutlet weak var userNameTF: UITextField!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(notification:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notificaton:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
// Do any additional setup after loading the view, typically from a nib.
}
@objc func keyboardWillShow(notification:Notification)
{
print("keyboard will show")
if let info = notification.userInfo,let keyboardFrame = info [UIKeyboardFrameEndUserInfoKey] as? NSValue{
let frame = keyboardFrame.cgRectValue
bottomConstraint.constant = frame.size.height+10
UIView.animate(withDuration: 0.8, animations: {
self.view.layoutIfNeeded()
})
}
}
@objc func keyboardWillHide(notificaton:Notification)
{
bottomConstraint.constant = 40
UIView.animate(withDuration: 0.8, animations: {
self.view.layoutIfNeeded()
})
}
//deregestering the observers
deinit {
NotificationCenter.default.removeObserver(self)
}
}
//textfield degate in extension or can be writen in class
extension UIViewController:UITextFieldDelegate{
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
Sign up here with your email
ConversionConversion EmoticonEmoticon