Search code examples
iosuiviewsubviewuiview-hierarchy

How to list out all the subviews in a uiviewcontroller in iOS?


I want to list out all the subviews in a UIViewController. I tried self.view.subviews, but not all of the subviews are listed out, for instance, the subviews in the UITableViewCell are not found. Any idea?


Solution

  • You have to recursively iterate the sub views.

    - (void)listSubviewsOfView:(UIView *)view {
        
        // Get the subviews of the view
        NSArray *subviews = [view subviews];
    
        for (UIView *subview in subviews) {
            
            // Do what you want to do with the subview
            NSLog(@"%@", subview);
    
            // List the subviews of subview
            [self listSubviewsOfView:subview];
        }
    }