mahdi
mahdi

Reputation: 459

How to reposition the textfields after hiding the keyboard?

I am pushing the textfields up when typing because the keyboard might cover the textfields.Every thing is working fine and the textfields are pushed, but when I'm hiding the keyboard, the textfield stays in its position and doesn't go to its regular position.

import UIKit

// to remove keyboared when tapping around it
extension UIViewController {
func hideKeyboaredWhenTappedAround(){
    let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)

}
func dismissKeyboard(){
    view.endEditing(true)
}
}
class SignInViewController: UIViewController, UITextFieldDelegate {
//MARK: Proporties
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contactUsButton: UIButton!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var forgotButton: UIButton!
@IBOutlet weak var registerButton: UIButton!
var activeField : UITextField? // #1
@IBOutlet weak var callButton: UIButton!

//MARK: View Cycle
override func viewDidLoad() {
    super.viewDidLoad()

    self.hideKeyboaredWhenTappedAround()
    usernameTextField.delegate = self
    passwordTextField.delegate = self

    // #1
    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)

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: Pressed buttons
@IBAction func contactUsButtonPressed(_ sender: Any) {
}

@IBAction func forgotButtonPressed(_ sender: Any) {
}
@IBAction func registerButtonPressed(_ sender: Any) {
    let registerVC = storyboard?.instantiateViewController(withIdentifier: "RegisterViewController") as! RegisterViewController
    self.navigationController?.pushViewController(registerVC, animated: true)

}

@IBAction func callButtonPressed(_ sender: Any) {
}

//MARK: To scroll keyboard
// to scroll the view up to use the keyboard #1
func keyboardWasShown(notification: NSNotification){
    //Need to calculate keyboard exact size due to Apple suggestions
    self.scrollView.isScrollEnabled = true
    var info = notification.userInfo!
    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 let activeField = self.activeField {
        if (!aRect.contains(activeField.frame.origin)){
            self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
        }
    }
}
// #1
func keyboardWillBeHidden(notification: NSNotification){
    //Once keyboard disappears, restore original positions
    var info = notification.userInfo!
    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
}

//MARK: TextField Delegate
func textFieldDidBeginEditing(_ textField: UITextField){
    activeField = textField
}

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

// func to hide keyboard when return is tapped
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}
}

Upvotes: 1

Views: 43

Answers (1)

Martin S
Martin S

Reputation: 386

Have you tried IQKeyboardManager ?

Upvotes: 1

Related Questions