Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 549
user403015 Avatar asked Aug 30 '11 13:08

user403015


People also ask

How do I get Subviews in Uiview Swift?

If you need a quick way to get hold of a view inside a complicated view hierarchy, you're looking for viewWithTag() – give it the tag to find and a view to search from, and this method will search all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number.

What is UIViewController in IOS?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


2 Answers

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];     } } 
like image 165
EmptyStack Avatar answered Sep 21 '22 06:09

EmptyStack


The xcode/gdb built-in way to dump the view hierarchy is useful -- recursiveDescription, per http://developer.apple.com/library/ios/#technotes/tn2239/_index.html

It outputs a more complete view hierarchy which you might find useful:

> po [_myToolbar recursiveDescription]  <UIToolbarButton: 0xd866040; frame = (152 0; 15 44); opaque = NO; layer = <CALayer: 0xd864230>>    | <UISwappableImageView: 0xd8660f0; frame = (0 0; 0 0); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xd86a160>> 
like image 29
natbro Avatar answered Sep 19 '22 06:09

natbro