Created
July 3, 2015 16:13
-
-
Save BradB132/2720fec149a9e5b190fd to your computer and use it in GitHub Desktop.
InterfaceBuilderTricks-1
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
| private var stringTagHandle: UInt8 = 0 | |
| extension UIView { | |
| //use Objective C Associated Object API to add this property to UIView | |
| @IBInspectable public var stringTag:String? { | |
| get { | |
| if let object = objc_getAssociatedObject(self, &stringTagHandle) as? String { | |
| return object | |
| } | |
| return nil | |
| } | |
| set { | |
| objc_setAssociatedObject(self, &stringTagHandle, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC)) | |
| } | |
| } | |
| //this should work in a similar way to viewWithTag: | |
| public func viewWithStringTag(strTag:String) -> UIView? { | |
| if stringTag == strTag { | |
| return self | |
| } | |
| for view in subviews as! [UIView] { | |
| if let matchingSubview = view.viewWithStringTag(strTag) { | |
| return matchingSubview | |
| } | |
| } | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment