0

I'm having a problem with keyboard textfield. I'm working on swift 4 in iOS 9.2

I have a login screen which does have a lot of constraints (to be adaptable for every screen). In my login screen, I have a username and password textfield.

The problem is that when I launch the application on my IPad, the keyboard appears in center of the associated textField. Moreover, the observer won't work : When I use debugger, I never get inside the observers

But when I launch on simulator, everything works perfectly.

#My object life cycle 
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(self, selector:  #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}


# My textfield delegates and observers
extension LoginViewController {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}


@objc func keyboardWillShow(_ notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height
        }
    }
}

@objc func keyboardWillHide(_ notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}

}

And for my textfield storyboard configuration, here is it

Look textField control textfield constraint

So what didn't I understood ?

  • Is the keyboard also dependants to textfield constraints ?
  • Why the observers aren't working on iPad but works in simulation ?
  • how can I move the keyboard programatically ?
Sacha.R
  • 394
  • 2
  • 17

3 Answers3

0

Make sure your iPad/iPhone have the keyboard dock..

https://discussions.apple.com/thread/5430957

Sacha.R
  • 394
  • 2
  • 17
0

You're not inheriting from UITextFieldDelegate in your extension. Also, make sure you set your textfields as self delegate in viewDidLoad method.

@IBOutlet weak var yourViewName: UIView!

override func viewDidLoad() {

   loginTextField.delegate = self
   passwordTextField.delegate = self

   super.viewDidLoad()

   NotificationCenter.default.addObserver(self, selector:#selector(LoginViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

   NotificationCenter.default.addObserver(self, selector:#selector(LoginViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)

}

extension LoginViewController : UITextFieldDelegate {

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    @objc func keyboardWillShow(notification: NSNotification) {
        UIView.animate(withDuration: 0.3) {
            self.usernameField.frame.origin.y -= 43
            self.usernameLine.frame.origin.y -= 43
            self.passwordField.frame.origin.y -= 43
            self.passwordLine.frame.origin.y -= 43
            self.loginBtn.frame.origin.y -= 65
        }
    }

    @objc func keyboardWillHide(notification: NSNotification) {
        UIView.animate(withDuration: 0.3) {
            self.usernameField.frame.origin.y += 33
            self.usernameLine.frame.origin.y += 33
            self.passwordField.frame.origin.y += 33
            self.passwordLine.frame.origin.y += 33
            self.loginBtn.frame.origin.y += 55
        }
    }
Comrade
  • 81
  • 5
0

easies way , you first take one scroll view and add top , bottom , leading , trailing contains of all 4 with value 0 and than take one view and add 4 constrain for view to top,bottom,leading,traling to scrollview and put your textfield inside that view and add constrains as per your design concept relative to view which you added in this scrollview ,

now , whenever your keyboard will open your view will move toward upside and textfield and keyboard both will be look properly