Skip to content

Instantly share code, notes, and snippets.

@RamonGilabert
Last active March 26, 2016 19:34
Show Gist options
  • Save RamonGilabert/0ffe1f196a371d44e3d9 to your computer and use it in GitHub Desktop.
Save RamonGilabert/0ffe1f196a371d44e3d9 to your computer and use it in GitHub Desktop.
If you are creating your constraints without any pod, this is a small convenience to not having to set the property every time.
import UIKit
extension UIView {
override public class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
let originalSelector = #selector(willMoveToSuperview)
let swizzledSelector = #selector(swizzle_willMoveToSuperview)
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
func swizzle_willMoveToSuperview() {
swizzle_willMoveToSuperview()
translatesAutoresizingMaskIntoConstraints = false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment