简体   繁体   中英

Adjust View for keyboard appears when switching UITextField (Swift)

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.

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
}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM