Skip to content

Instantly share code, notes, and snippets.

@anivaros
Last active August 29, 2015 14:08
Show Gist options
  • Save anivaros/9992c12337f17da7dd83 to your computer and use it in GitHub Desktop.
Save anivaros/9992c12337f17da7dd83 to your computer and use it in GitHub Desktop.
Swizzled -[UIView addSubvew:]
+ (void)loadSwizzle {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
// When swizzling a class method, use the following:
// Class class = object_getClass((id)self);
SEL originalSelector = @selector(addSubview:);
SEL swizzledSelector = @selector(my_addSubview:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)my_addSubview:(UIView *)view {
[self my_addSubview:view];
// For me normal state is 2 subviews on root view
if ([UIApplication sharedApplication].delegate.window.rootViewController.view.subviews.count > 2) {
[Log addMessage:@"%s -> %@",__PRETTY_FUNCTION__, [NSThread callStackSymbols]];
[Log addMessage:@"Count of subviews: %d",[UIApplication sharedApplication].delegate.window.rootViewController.view.subviews.count];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment