Created
July 9, 2018 12:46
-
-
Save kevinmlong/15d9a25eab576de29ad8b3d332f326ef to your computer and use it in GitHub Desktop.
Unoccluded UITextField Extension (Text Field Delegate)
This file contains 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
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