0

I am trying to write a code to ensure UITextField move up when keyboard present. I have written a code based on what I found in stack overflow however it is not working. The original code was written in objective-c but I am not familiar with the it and hence decided to to write code in swift.

can anyone tell what I may be doing wring here?

 {

        // moving text box up when tyoing
func registerForKeyboardNotifications()
{
    //Adding notifies on keyboard appearing
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}



@objc func keyboardWasShown(notification: NSNotification)
{
    //Need to calculate keyboard exact size due to Apple suggestions

    self.scrollView.isScrollEnabled = true
    let info : NSDictionary = notification.userInfo! as NSDictionary
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets

    var aRect : CGRect = self.view.frame
    aRect.size.height -= keyboardSize!.height
    if (yourEmail) != nil
    {
        if (!aRect.contains(yourEmail!.frame.origin))
        {
            self.scrollView.scrollRectToVisible(yourEmail!.frame, animated: true)
        }
    }


}


@objc func keyboardWillBeHidden(notification: NSNotification)
{
    //Once keyboard disappears, restore original positions
    let info : NSDictionary = notification.userInfo! as NSDictionary as NSDictionary
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    self.view.endEditing(true)
    self.scrollView.isScrollEnabled = false

}

func textFieldDidBeginEditing(_ textField: UITextField)
{
    yourEmail = textField
}

func textFieldDidEndEditing(_ textField: UITextField)
{
    yourEmail = nil
}





 }
Pratik Jamariya
  • 750
  • 1
  • 8
  • 34
  • Possible duplicate of [How to make a UITextField move up when keyboard is present?](https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Rishu Gupta Nov 19 '17 at 12:42
  • Hi Rishu, I am trying to write the code in swift .. plus I have wrote the code but textfield not moving up whenever it is hiding behind keyboard ... I don't why ? – Akhil Prasad Godiyal Nov 22 '17 at 03:58
  • Is your textfield inside scrollview ? – Rishu Gupta Nov 22 '17 at 04:17

2 Answers2

0

Try this code

func textFieldDidBeginEditing(textField: UITextField!)
{
     self.scrollView.setContentOffset(CGPoint.init(x: 0, y: scrollBy), animated: true)
  // scrollBy - pass the height you want your scrollview to be scrolled when keyboard appears
 }

func textFieldDidEndEditing(textField: UITextField!)
{
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

    self.view.endEditing(true);
}
0

below is the code I wrote with the help of suggestions:

{

func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField.frame.maxY > self.view.frame.height * 0.6
    {
        self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - self.view.frame.height * 0.6 + 2.0), animated: true)

    }
    else{
        return
    }

}

func textFieldDidEndEditing(_ textField: UITextField)
{
    self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)

    self.view.endEditing(true);
}

}