Last active
August 29, 2015 14:08
-
-
Save anivaros/9992c12337f17da7dd83 to your computer and use it in GitHub Desktop.
Swizzled -[UIView addSubvew:]
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
+ (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