Skip to content

Instantly share code, notes, and snippets.

@ramiresnas
Created April 26, 2018 02:35
Show Gist options
  • Select an option

  • Save ramiresnas/ef3bd61b6f405f0382a36268403ef46b to your computer and use it in GitHub Desktop.

Select an option

Save ramiresnas/ef3bd61b6f405f0382a36268403ef46b to your computer and use it in GitHub Desktop.
controle do teclado
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)
}
}
@ramiresnas
Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment