Kishor Pahalwani
Kishor Pahalwani

Reputation: 1022

Keyboard move up and down

I have a loginView, inside that i have two textFields and a button. I have to move up the view while tapping on textField and move down the view when press the return key.

My problem is that it is working fine for all conditions but while tap on the one textField view is moving up but at same time when we go for next textField view is moving down.

import UIKit
class CheckFontIconView: UIViewController,UITextFieldDelegate {

    var activeField: UITextField?

    @IBOutlet weak var loginFieldsView: UIView!

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var mobileNo: UITextField!
    @IBOutlet weak var textFieldPassword: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        mobileNo.delegate = self
        textFieldPassword.delegate = self

        registerForKeyboardNotifications()
    }

    @IBAction func btnLoginAction(_ sender: Any) {

    }

    deinit {

        //NotificationCenter.default.removeObserver(self)
        self.deregisterFromKeyboardNotifications()
    }


    func registerForKeyboardNotifications() {

        //Adding notifies on keyboard appearing
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func deregisterFromKeyboardNotifications() {

        //Removing notifies on keyboard appearing
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func keyboardWasShown(notification: NSNotification) {

        //Need to calculate keyboard exact size due to Apple suggestions
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        if self.activeField != nil {
            self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
        }
    }

    func keyboardWillBeHidden(notification: NSNotification) {

        //Once keyboard disappears, restore original positions
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
        self.loginFieldsView.endEditing(true)
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.activeField = textField
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        self.activeField = nil

    }

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

Upvotes: 0

Views: 130

Answers (4)

Deepak Kumar Sahu
Deepak Kumar Sahu

Reputation: 453

Once try with scrollView,I hope it will help you.

Use a scroll view to moving text field up and down.

Making Up

  1. Make a @IBOutlet weak var scroller: UIScrollView!;
  2. Under textFieldDidBeginEditing:textField method
  3. Create a CGPoint with x: 0 and y texfield.frame.origin.y
  4. And then start moving the scroller with setContentOffset:animated function.(Parameter will be your CGPoint and a boolean value true).

Making Down

  1. Under textFieldDidEndEditing:textField set your scroller to CGPoint.zero with same setContentOffset:animated function.

*And your textFieldShouldReturn:textField should be resignFirstResponder or to next textField.

Please let me know if having any problem on this.

Upvotes: 1

Rohit Khandelwal
Rohit Khandelwal

Reputation: 1778

Just one change hold your view old frame in a variable and assign it again to your view when keyboard disappear.

import UIKit
    class CheckFontIconView: UIViewController,UITextFieldDelegate {

var activeField: UITextField?

@IBOutlet weak var loginFieldsView: UIView!

@IBOutlet weak var label: UILabel!
@IBOutlet weak var mobileNo: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
var previousFrame : CGRect!
override func viewDidLoad() {
    super.viewDidLoad()

    previousFrame=self.loginFieldsView.frame

    mobileNo.delegate = self
    textFieldPassword.delegate = self

    registerForKeyboardNotifications()
}

func keyboardWasShown(notification: NSNotification) {

    //Need to calculate keyboard exact size due to Apple suggestions
    var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    if self.activeField != nil {
        self.loginFieldsView.frame.origin.y = previousFrame.origin.y-(keyboardSize?.height)!
    }
}

func keyboardWillBeHidden(notification: NSNotification) {

    //Once keyboard disappears, restore original positions
    /*var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size*/
    self.loginFieldsView.frame = previousFrame
    //self.loginFieldsView.endEditing(true)
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    self.activeField = textField
}

func textFieldDidEndEditing(_ textField: UITextField) {
    self.activeField = nil

}

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

Upvotes: 0

nynohu
nynohu

Reputation: 1668

I made the simple code for your problem. It is not good but you can refer with that idea for fix your problem.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var activeField: UITextField?

    @IBOutlet weak var loginFieldsView: UIView!

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var mobileNo: UITextField!
    @IBOutlet weak var textFieldPassword: UITextField!
    var originalHeight:CGFloat!

    override func viewDidLoad() {
        super.viewDidLoad()

        mobileNo.delegate = self
        textFieldPassword.delegate = self
        originalHeight = self.loginFieldsView.frame.origin.y

        registerForKeyboardNotifications()
    }

    @IBAction func btnLoginAction(_ sender: Any) {

    }

    deinit {

        //NotificationCenter.default.removeObserver(self)
        self.deregisterFromKeyboardNotifications()
    }


    func registerForKeyboardNotifications() {

        //Adding notifies on keyboard appearing
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func deregisterFromKeyboardNotifications() {

        //Removing notifies on keyboard appearing
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func keyboardWasShown(notification: NSNotification) {

        //Need to calculate keyboard exact size due to Apple suggestions
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        if self.activeField != nil {
            if self.loginFieldsView.frame.origin.y == originalHeight {
                self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
            }
        }
    }

    func keyboardWillBeHidden(notification: NSNotification) {

        //Once keyboard disappears, restore original positions
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        if originalHeight > self.loginFieldsView.frame.origin.y {
            self.loginFieldsView.frame.origin.y += (keyboardSize?.height)!
        }
        self.loginFieldsView.endEditing(true)
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.activeField = textField
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        self.activeField = nil

    }

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

Upvotes: 0

Hitesh Surani
Hitesh Surani

Reputation: 13567

Thanks for ask question...

Use TPKeyboard to achieve above functionality then no manage this kind of stuff (Automatically manage by TPKeyboard)...

Update:

I think you have done some mistake in given below code...

func textFieldDidEndEditing(_ textField: UITextField) {
        self.activeField = nil
 }

You have to DEBUG and identify exact issue..

Happy coding...

Upvotes: 0

Related Questions