Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kevinmlong/15d9a25eab576de29ad8b3d332f326ef to your computer and use it in GitHub Desktop.
Save kevinmlong/15d9a25eab576de29ad8b3d332f326ef to your computer and use it in GitHub Desktop.
Unoccluded UITextField Extension (Text Field Delegate)
import UIKit
import ObjectiveC
struct AssociatedKeys {
static var activeTextField: UInt8 = 0
}
extension UIViewController : UITextFieldDelegate {
private(set) var activeTextField: UITextField? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.activeTextField) as? UITextField
}
set(newValue) {
objc_setAssociatedObject(self, &AssociatedKeys.activeTextField, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
public func textFieldDidBeginEditing(_ textField: UITextField) {
self.activeTextField = textField
}
public func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
self.activeTextField = nil
}
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment