override func viewDidLoad() {
super.viewDidLoad()
self.submitBtn.translatesAutoresizingMaskIntoConstraints = true
emailTf.text = emailID
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
// Do any additional setup after loading the view.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(PMSignupViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}
@objc func dismissKeyboard()
{
view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func keyboardWillShow(notification: NSNotification) {
print("keyboardWillShow")
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: true)
}
@objc func keyboardWillHide(notification: NSNotification){
print("keyboardWillHide")
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: false)
}
func moveButton(_ button: UIButton, moveDistance: Int, up: Bool) {
let moveDuration = 0.3
let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(moveDuration)
self.submitBtn.frame = self.submitBtn.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
@IBAction func submitBtnAction(_ sender: Any) {
}
}
So basically I have a view with a textfield on top and a button in the bottom. When my keyboard is up the button moves up along with it. But the button doesn't trigger the action method while it is on top of the keyboard. But when the keyboard is dismissed and its back to the bottom of the screen it works fine. Any input on this?