43

Here is a helpful routine you can use in an iOS app to traverse the view hierarchy when you want to know what's being created and where it's going.

This routine dumps the view hierarchy to NSLog() starting at the view passed in. Subviews show their index in the subviews array and all super classes in order separated by a colon with the frame size at the end.

To dump the entire view hierarchy of your application, call the method like this:

dumpViews([[UIApplication sharedApplication] keyWindow], @"", @"");

To display the hierarchy of the the camera view, override this method in your controller: navigationController:willShowViewController:viewController:animated:

and call the dump routine like this:

dumpViews(self.modalViewController.view, @"", @"");

For all other views:

dumpViews(myView, @"", @"");


Source

void dumpViews(UIView* view, NSString *text, NSString *indent) 
{
    Class cl = [view class];
    NSString *classDescription = [cl description];
    while ([cl superclass]) 
    {
        cl = [cl superclass];
        classDescription = [classDescription stringByAppendingFormat:@":%@", [cl description]];
    }

    if ([text compare:@""] == NSOrderedSame)
        NSLog(@"%@ %@", classDescription, NSStringFromCGRect(view.frame));
    else
        NSLog(@"%@ %@ %@", text, classDescription, NSStringFromCGRect(view.frame));

    for (NSUInteger i = 0; i < [view.subviews count]; i++)
    {
        UIView *subView = [view.subviews objectAtIndex:i];
        NSString *newIndent = [[NSString alloc] initWithFormat:@"  %@", indent];
        NSString *msg = [[NSString alloc] initWithFormat:@"%@%d:", newIndent, i];
        dumpViews(subView, msg, newIndent);
        [msg release];
        [newIndent release];
    }
}




Example Camera Dump

UIView:UIResponder:NSObject {{0, 0}, {320, 480}}
  0: PLCameraView:UIView:UIResponder:NSObject {{0, 0}, {320, 480}}
    0: UIView:UIResponder:NSObject {{0, 0}, {320, 427}}
    1: UIImageView:UIView:UIResponder:NSObject {{10000, 10000}, {320, 480}}
    2: UIView:UIResponder:NSObject {{0, 427}, {320, 53}}
    3: PLCropOverlay:UIView:UIResponder:NSObject {{0, 0}, {320, 480}}
      0: UIImageView:UIView:UIResponder:NSObject {{0, 20}, {320, 96}}
      1: PLCropLCDLayer:UIView:UIResponder:NSObject {{0, 20}, {320, 96}}
      2: TPBottomDualButtonBar:TPBottomButtonBar:TPBottomBar:UIView:UIResponder:NSObject {{0, 384}, {320, 96}}
        0: TPPushButton:UIThreePartButton:UIPushButton:UIControl:UIView:UIResponder:NSObject {{22, 26}, {128, 47}}
        1: TPCameraPushButton:TPPushButton:UIThreePartButton:UIPushButton:UIControl:UIView:UIResponder:NSObject {{170, 26}, {128, 47}}
          0: UIImageView:UIView:UIResponder:NSObject {{51, 12}, {26, 19}}
jscs
  • 63,694
  • 13
  • 151
  • 195
Sophtware
  • 1,796
  • 2
  • 21
  • 34
  • 7
    I've asked on Meta about what we should do about this question: https://meta.stackoverflow.com/q/378604/1709587. You may wish to weigh in there. – Mark Amery Jan 05 '19 at 17:24
  • 5
    [You are allowed and even encouraged to self-answer your own problem](https://stackoverflow.com/help/self-answer), and the community feels it's useful, but considering this site is a Q&A site (not a blog, etc.), consider fixing it to suit this site's format by editing the question to include the background problem, then moving the answer in the question to the answer (of course, when the question has been reopened). Otherwise, this could give other people a wrong impression that they can share any code they want on here without any reason. – Andrew T. Jan 05 '19 at 18:54

0 Answers0