Last active
August 23, 2017 19:21
-
-
Save tsraveling/d1a151fded8dd9fa5d35c5ee8422e911 to your computer and use it in GitHub Desktop.
UITextField delegate
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
| // MARK: - UITextFieldDelegate - | |
| func textFieldDidBeginEditing(_ textField: UITextField) { | |
| let screen_offset = (UIScreen.main.bounds.size.height - 330) - 60 // Gets offset based on screen size and keyboard / nav height | |
| let y = textField.frame.origin.y - screen_offset | |
| self.scrollView.setContentOffset(CGPoint(x: 0, y: y > 0 ? y : 0), animated: true) | |
| } | |
| func textFieldDidEndEditing(_ textField: UITextField) { | |
| } | |
| func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | |
| let text = textField.text ?? "" | |
| let updatedText = (text as NSString).replacingCharacters(in: range, with: string) as String | |
| // Do something with updatedText ... | |
| return true | |
| } | |
| func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
| textField.resignFirstResponder() | |
| return true | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment