Skip to content

Instantly share code, notes, and snippets.

@vlastachu
Created April 26, 2016 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlastachu/a34aa547c203ad04c8d5611b6b46f5a5 to your computer and use it in GitHub Desktop.
Save vlastachu/a34aa547c203ad04c8d5611b6b46f5a5 to your computer and use it in GitHub Desktop.
//...
// change instance property setter to custom at runtime
let type = self.view.superview!.superview!.superview!.dynamicType
if String.fromCString(class_getName(type)) != "runtimeUIView" {
let newtype = objc_allocateClassPair(type, "runtimeUIView", 0)
let imp = class_getMethodImplementation(ViewController.self, #selector(ViewController.fakeFrameSetter(_:)))
let types = method_getTypeEncoding(class_getInstanceMethod(ViewController.self, #selector(ViewController.fakeFrameSetter(_:))))
class_addMethod(newtype, Selector("setFrame:"), imp, types)
object_setClass(self.view.superview!.superview!.superview!, newtype)
}
public func fakeFrameSetter(value: CGRect) {
// should do nothing but print is useful for debug
print("don't fucking touch my frame, you piece of cunt!")
}
// change class property setter to custom at runtime
let imp = class_getMethodImplementation(ViewController.self, #selector(ViewController.fakeFrameSetter(_:)))
let type = self.view.superview!.superview!.superview!.dynamicType
var uselessVariable:UInt32 = 0
withUnsafeMutablePointer(&uselessVariable) { ptr in
var methods: UnsafeMutablePointer<Method> = class_copyMethodList(type, ptr)
for _ in 0 ... (ptr.memory) {
let m: Method = methods.memory
if method_getName(m) == Selector("setFrame:") {
method_setImplementation(m, imp)
}
methods = methods.advancedBy(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment