0

I have two UIView's , the Main one and one which will move up when the keyboard appears (Second UIView has two textfields). I've managed to do this with the following code:

LoginViewController.swift :

override func viewDidLoad() {
    super.viewDidLoad()


    self.originalCenter = self.contentView.center;
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)


}

func keyboardDidShow(notification: NSNotification)
{
    UIView.animateWithDuration(1.0, animations: {
    self.contentView.center = CGPointMake(self.originalCenter.x, self.originalCenter.y - 40);
     })

}

My big problems is that because the movement of the UIView depends on the keyboard notification, when the user taps one TextField it moves up as it should, but when he taps the second one the view moves down automatically. What Im I doing wrong?

This is what is happening: Keyboard Error Gif

This question was answered over here , but I need the solution for Swift language.

Community
  • 1
  • 1
Omar Dlhz
  • 158
  • 12

2 Answers2

0

Create an instance variable and check whether the keyboard was activated or not: class ViewController: UIViewController, UITextFieldDelegate {

var _keyboardActivated: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    registerForKeyboardNotifications()
}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self,
        name: UIKeyboardDidShowNotification,
        object: nil)

    NSNotificationCenter.defaultCenter().removeObserver(self,
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func registerForKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "keyboardWillShow:",
        name: UIKeyboardWillShowNotification,
        object: nil)

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "keyboardWillBeHidden:",
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func keyboardWillShow(notification: NSNotification) {
    if (!_keyboardActivated) {
      // do stuff
    }
    _keyboardActivated = true
}

func keyboardWillBeHidden(notification: NSNotification) {
    _keyboardActivated = false
}
}
Teodor Ciuraru
  • 3,417
  • 1
  • 32
  • 39
0

Set delegate of your textfield and implement the below methods

func textFieldDidBeginEditing(textField: UITextField) {
        animateViewMoving(true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
        animateViewMoving(false, moveValue: 100)
}

func animateViewMoving (up:Bool, moveValue :CGFloat){
    var movementDuration:NSTimeInterval = 0.3
    var movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    UIView.commitAnimations()
}

Reference: http://www.jogendra.com/2015/01/uitextfield-move-up-when-keyboard.html

pkc456
  • 8,350
  • 38
  • 53
  • 109