隐藏键盘后如何重新定位文本字段?

时间:2017-01-21 21:35:49

标签: ios swift scrollview

我在打字时推文字区域,因为键盘可能会覆盖文本字段。每件事都运行正常并且文本字段被推送,但是当我隐藏键盘时,文本字段保持在其位置并且没有&# 39;去常规位置。

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

1 个答案:

答案 0 :(得分:1)

您是否尝试过IQKeyboardManager