Forked from steipete/PSPDFKitImproveRecursiveDescription.m
Created
July 1, 2012 14:40
-
-
Save catlan/3028618 to your computer and use it in GitHub Desktop.
pre iOS 4.3
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
# You need #import <objc/runtime.h> | |
#ifdef DEBUG | |
void pspdf_swizzle(Class c, SEL orig, SEL new) { | |
Method origMethod = class_getInstanceMethod(c, orig); | |
Method newMethod = class_getInstanceMethod(c, new); | |
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod))) { | |
class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); | |
}else { | |
method_exchangeImplementations(origMethod, newMethod); | |
} | |
} | |
NSString* pspdf_customViewDescription(id _self, SEL _cmd) { | |
SEL customViewDescriptionSEL = NSSelectorFromString(@"pspdf_customViewDescription"); | |
NSString *description = [_self performSelector:customViewDescriptionSEL]; | |
SEL viewDelegateSEL = NSSelectorFromString([NSString stringWithFormat:@"%@ewDelegate", @"_vi"]); // pr. API | |
if ([_self respondsToSelector:viewDelegateSEL]) { | |
UIViewController *viewController = [_self performSelector:viewDelegateSEL]; | |
NSString *viewControllerClassName = NSStringFromClass([viewController class]); | |
if ([viewControllerClassName length]) { | |
NSString *children = @""; // iterate over childViewControllers | |
if ([viewController respondsToSelector:@selector(childViewControllers)] && [viewController.childViewControllers count]) { | |
description = [NSString stringWithFormat:@"%d children:[ ", [viewController.childViewControllers count]]; | |
for (UIViewController *childViewController in viewController.childViewControllers) { | |
description = [NSString stringWithFormat:@"%@%@:%p ", description, NSStringFromClass([childViewController class]), childViewController]; | |
} | |
description = [description stringByAppendingString:@"]"]; | |
} | |
// check if the frame of a childViewController is bigger than the one of a parentViewController. (usually this is a bug) | |
NSString *warnString = @""; | |
if (viewController && viewController.parentViewController && [viewController isViewLoaded] && [viewController.parentViewController isViewLoaded]) { | |
CGRect parentRect = viewController.parentViewController.view.bounds; | |
CGRect childRect = viewController.view.frame; | |
if (parentRect.size.width < childRect.origin.x + childRect.size.width || | |
parentRect.size.height < childRect.origin.y + childRect.size.height) { | |
warnString = @"OVERLAPPING PARENT! "; | |
} | |
} | |
description = [NSString stringWithFormat:@"%@'%@:%p'%@ %@", warnString, viewControllerClassName, viewController, children, description]; | |
} | |
} | |
return description; | |
} | |
__attribute__((constructor)) static void PSPDFKitImproveRecursiveDescription(void) { | |
// Following code patches UIView's description to show the classname of an an view controller, if one is attached. | |
// Will only get compiled for debugging. Use 'po [[UIWindow keyWindow] recursiveDescription]' to invoke. | |
SEL customViewDescriptionSEL = NSSelectorFromString(@"pspdf_customViewDescription"); | |
class_addMethod([UIView class], customViewDescriptionSEL, (IMP)pspdf_customViewDescription, "@@:"); | |
pspdf_swizzle([UIView class], @selector(description), customViewDescriptionSEL); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment