Search code examples
iosobjective-cuiviewuiview-hierarchy

Write the UIView Hierarchy


I’m trying to write a text file with name of all the views contains in the Screen. For example I have a UIView in which I have 2 UIButtons so the file should be as bellow..

UIWindow
    UIView
        UIButton
            UIButtonLabel
        UIButton
            UIButtonLabel
        _UILayoutGuide
        _UILayoutGuide

This is how I'm trying to do but the above hierarchy I'm unable to achieve.

-(void) writeViewHierarchyForView:(UIView *)aView inString:( NSMutableString *)str
{
    //NSMutableString *str = [[NSMutableString alloc] init];
    NSLog(@"title Text = %@", aView.titleText);
    if(nil == aView.children){
        [str appendString:@"\n"];
        return;
    }
    else{
        for(UIView *view in aView.subview){
            [str appendString:@"\n\t"];
            [str appendString:view.name];
            NSLog(@"%@",view.name);
            [str appendString:@"\t"];
            [self writeViewHierarchyForView:view inString:str];
        }
    }
}

this is not something I'm using in any project but doing it just for fun. any suggestion would be great.


Solution

  • You probably won't be able to submit apps to the app store using the following code (which it sounds like you don't want anyways) since it's undocumented private API, but you can use this in your debugging:

    NSLog(@"%@", [self.view valueForKey:@"recursiveDescription"]);

    you can also use recursiveDescription in the LLDB console for debugging when you're on a breakpoint:

    po [someView recursiveDescription]