Last active
February 21, 2020 11:13
-
-
Save robnadin/0aa8808266e8b33e1c4fca7d4c79540f to your computer and use it in GitHub Desktop.
Add support for custom layout guide instances in interface builder by using custom UIView subclass. Uses method swizzling to swap out the view during nib decoding.
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
private extension UIView { | |
private struct Static { | |
static let initialize: (AnyClass) -> Void = { (aClass: AnyClass!) in | |
let swizzle: (Selector, Selector) -> Void = { (original, swizzled) in | |
let originalSelector = original | |
let swizzledSelector = swizzled | |
let originalMethod = class_getInstanceMethod(aClass, originalSelector) | |
let swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector) | |
let didAddMethod = class_addMethod(aClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) | |
if didAddMethod { | |
class_replaceMethod(aClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)) | |
} else { | |
method_exchangeImplementations(originalMethod, swizzledMethod) | |
} | |
} | |
let pairs = [ | |
#selector(UIView.addSubview(_:)): #selector(UIView.layoutGuide_addSubview(_:)) | |
] | |
pairs.forEach { (key, value) in | |
swizzle(key, value) | |
} | |
} | |
} | |
open override class func initialize() { | |
// make sure this isn't a subclass | |
if self !== UIView.self { | |
return | |
} | |
Static.initialize(self) | |
} | |
private dynamic func layoutGuide_addSubview(_ view: AnyObject!) { | |
if let layoutGuide = view as? UILayoutGuide { | |
addLayoutGuide(layoutGuide) | |
return | |
} | |
layoutGuide_addSubview(view) | |
} | |
} | |
class SpacerView: UIView { | |
@IBInspectable var identifier: String? | |
override func awakeAfter(using aDecoder: NSCoder) -> Any? { | |
return UILayoutGuide(coder: aDecoder) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment