Created
April 26, 2018 02:35
-
-
Save ramiresnas/ef3bd61b6f405f0382a36268403ef46b to your computer and use it in GitHub Desktop.
controle do teclado
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
| class ViewController: UIViewController , UITextFieldDelegate{ | |
| @IBOutlet weak var scrollView : UIScrollView! | |
| @IBOutlet var textFields: [UITextField]! | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow , object: nil) | |
| NotificationCenter.default.addObserver(self, selector: #selector(keyboarbWillHide), name: .UIKeyboardWillHide , object: nil) | |
| textFields.forEach{$0.delegate = self} | |
| let tap = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard) ) | |
| scrollView.addGestureRecognizer(tap) | |
| } | |
| @objc func hideKeyboard(){ | |
| view.endEditing(true) | |
| } | |
| func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
| textField.resignFirstResponder() | |
| return true | |
| } | |
| @objc func keyboardWillShow(_ notification : Notification ){ | |
| let point = CGPoint(x: 0, y: 250) | |
| scrollView.setContentOffset(point, animated: true) | |
| } | |
| @objc func keyboarbWillHide(_ notification : Notification ){ | |
| scrollView.setContentOffset(.zero, animated: true) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
eu não estou satisfeito com o número mágico na linha 28, mas tem como pegar esse valor
notification.userInfo?[UIKeyboardFrameEndUserInfoKey]porém o valor é maior do que eu queria que fosse, mas vou estudar mais.