Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug view hierarchy in Xcode 7.3 fails

Tags:

This function fails with runtime error:

-[UIWindow viewForFirstBaselineLayout]: unrecognized selector sent to instance 0x7fb9dae257d0 

Anybody encountered the same?

UPD:
Fails on simulator iOS 8.1/8.4. 9.3 works fine.

UPD2: UIWindow is created like:

window = UIWindow(frame: UIScreen.mainScreen().bounds)     window?.rootViewController = RootViewController.rootVC window?.makeKeyAndVisible() 
like image 226
orkenstein Avatar asked Mar 30 '16 15:03

orkenstein


1 Answers

I got the view debugger working again by placing the following fix in my project:

#ifdef DEBUG  #import <UIKit/UIKit.h> #import <objc/runtime.h>  @implementation UIView (FixViewDebugging)  + (void)load {     Method original = class_getInstanceMethod(self, @selector(viewForBaselineLayout));     class_addMethod(self, @selector(viewForFirstBaselineLayout), method_getImplementation(original), method_getTypeEncoding(original));     class_addMethod(self, @selector(viewForLastBaselineLayout), method_getImplementation(original), method_getTypeEncoding(original)); }  @end  #endif 

When your project loads, the load method will execute, causing viewForFirstBaselineLayout and viewForLastBaselineLayout to use the viewForBaselineLayout implementation if they are not currently implemented, so view debugging gets iOS8 flavor the behavior it was looking for.

To add this to your own project, create a new empty Objective-C file in your project and paste the contents in. You can name it whatever you want. I call mine "UIView+FixViewDebugging". If you are in a pure Swift project you do not need to create a bridging header. The file will be compiled into your project and you don't need to reference it.

Note this will only work for debug builds because of the #ifdef DEBUG. You can remove it but then you may accidentally compile this into your release builds (though it should have no ill side effects). If this method isn't working with these lines, check that your target has DEBUG=1 in Build Settings > Apple LLVM - Preprocessing > Preprocessor Macros > Debug.

like image 186
Brian Nickel Avatar answered Oct 05 '22 22:10

Brian Nickel