Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catlan/3028618 to your computer and use it in GitHub Desktop.
Save catlan/3028618 to your computer and use it in GitHub Desktop.
pre iOS 4.3
# 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