2017-01-21 40 views
0

由於鍵盤可能會覆蓋文本框,因此我在文本框上打字。每件東西工作正常,並且文本框被壓入,但當我隱藏鍵盤時,文本框停留在其位置上,並且不會轉到它的正常位置。隱藏鍵盤後如何重新定位文本框?

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

回答