Last active
June 5, 2018 21:25
-
-
Save abhimuralidharan/92878f14e925f9844fcf9bd81adee78b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // SignupViewController.swift | |
| // TestApp | |
| // | |
| // Created by Abhilash on 25/02/17. | |
| // | |
| import UIKit | |
| class SignupViewController: UIViewController, UITextFieldDelegate { | |
| //MARK: - IBOutlet: | |
| @IBOutlet weak var emailTF: FloatingTF! | |
| @IBOutlet weak var passwordTF: FloatingTF! | |
| @IBOutlet weak var dobTF: FloatingTF! | |
| @IBOutlet weak var scrollView: UIScrollView! | |
| //MARK: - Variable: | |
| var activeTextField: UITextField! | |
| //MARK: - ViewController Lifecycle: | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| emailTF.delegate = self | |
| passwordTF.delegate = self | |
| dobTF.delegate = self | |
| } | |
| override func viewWillAppear(_ animated: Bool) { | |
| super.viewWillAppear(animated) | |
| registerKeyboardNotifications() | |
| } | |
| override func viewWillDisappear(_ animated: Bool) { | |
| super.viewWillDisappear(animated) | |
| deRegisterKeyboardNotifications() | |
| } | |
| //MARK: - Keyboard notification observer Methods | |
| fileprivate func registerKeyboardNotifications() { | |
| NotificationCenter.default.addObserver(self, selector: #selector(SignupViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) | |
| NotificationCenter.default.addObserver(self, selector: #selector(SignupViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) | |
| } | |
| @objc fileprivate fileprivate func deRegisterKeyboardNotifications() { | |
| NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow, object: nil) | |
| NotificationCenter.default.removeObserver(self, name: .UIKeyboardDidHide, object: nil) | |
| } | |
| @objc fileprivate func keyboardWillShow(notification: NSNotification) { | |
| if let activeTextField = activeTextField { // this method will get called even if a system generated alert with keyboard appears over the current VC. | |
| let info: NSDictionary = notification.userInfo! as NSDictionary | |
| let value: NSValue = info.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue | |
| let keyboardSize: CGSize = value.cgRectValue.size | |
| let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0) | |
| scrollView.contentInset = contentInsets | |
| scrollView.scrollIndicatorInsets = contentInsets | |
| // If active text field is hidden by keyboard, scroll it so it's visible | |
| // Your app might not need or want this behavior. | |
| var aRect: CGRect = self.view.frame | |
| aRect.size.height -= keyboardSize.height | |
| let activeTextFieldRect: CGRect? = activeTextField?.frame | |
| let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin | |
| if (!aRect.contains(activeTextFieldOrigin!)) { | |
| scrollView.scrollRectToVisible(activeTextFieldRect!, animated:true) | |
| } | |
| } | |
| } | |
| func keyboardWillHide(notification: NSNotification) { | |
| let contentInsets: UIEdgeInsets = .zero | |
| scrollView.contentInset = contentInsets | |
| scrollView.scrollIndicatorInsets = contentInsets | |
| } | |
| //MARK: - UITextField Delegate Methods | |
| func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
| if textField == emailTF { | |
| passwordTF.becomeFirstResponder() | |
| } | |
| else if textField == passwordTF { | |
| dobTF.becomeFirstResponder() | |
| } | |
| else { | |
| self.view.endEditing(true) | |
| } | |
| return true | |
| } | |
| func textFieldDidBeginEditing(_ textField: UITextField) { | |
| activeTextField = textField | |
| scrollView.isScrollEnabled = true | |
| } | |
| func textFieldDidEndEditing(_ textField: UITextField) { | |
| activeTextField = nil | |
| scrollView.isScrollEnabled = false | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment