Skip to content

Instantly share code, notes, and snippets.

@tsraveling
Last active August 23, 2017 19:21
Show Gist options
  • Save tsraveling/d1a151fded8dd9fa5d35c5ee8422e911 to your computer and use it in GitHub Desktop.
Save tsraveling/d1a151fded8dd9fa5d35c5ee8422e911 to your computer and use it in GitHub Desktop.
UITextField delegate
// 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