Get all hierarchies of UIView in iOS
Application Scenarios
In real IOS development, there are many times when you need to know which child controls are contained in a UI control, and to understand their hierarchies and their own frame and bounds, so that we can complete a complex UI layout, and the following code makes it easy to get all the hierarchies of a UI control, Use it to calculate, then write the result to the local disk, and export it to an XML file, so that we can visually see its internal details.
/** * Returns all hierarchies of incoming VEIW * *@param view needs to get a hierarchy of view * *@return String */-(NSString *) Digview: (UIView *) view{if ([View Iskindofclass:[uitableviewcellClass]])return @"";1. Initialize nsmutablestring *xml = [nsmutablestring string];2. label start [XML appendformat:@"<%@ frame=\"%@\ "", view.Class, Nsstringfromcgrect (View.frame)];if (! Cgpointequaltopoint (View.bounds.origin, Cgpointzero)) {[XML appendformat:@"Bounds=\"%@\ "", Nsstringfromcgrect (View.bounds)]; }if ([View Iskindofclass:[uiscrollviewClass]] {Uiscrollview *scroll = (Uiscrollview *) view; if (! Uiedgeinsetsequaltoedgeinsets (Uiedgeinsetszero, Scroll.contentinset)) {[XML appendformat:@ "contentinset=\"%@\ "", Nsstringfromuiedgeinsets (Scroll.contentinset)]; }} //3. Determine if you want to end if (View.subviews.count = 0" {[XML Appendstring:@return xml;} else {[XML appendstring:@ ">"];} //4. Traverse all child controls for (UIView *child in view.subviews) {NSString * Childxml = [self digview:child]; [XML Appendstring:childxml]; } //5. End of Tag [XML Appendformat:@ "</%@>", View.class]; return xml;}
Example: Very classic UITableView.
UITableViewCell *cell= (UITableViewCell *) [[[TextView Superview]superview] superview];
UITableView *mytableview= (UITableView *) [[(uitableviewcell*) [[[TextView Superview] superview] Superview] Superview] Superview];
UIView *myview = (uiview*) [Mytableview Superview];
(LLDB) po Cell
<ThreeElementsCell:0xc4eeab0; BaseClass = UITableViewCell; frame = (0 296; 768 44); AutoResize = W; Layer = <CALayer:0xc4ee060>>
(LLDB) po [textView Superview]
<UITableViewCellContentView:0xc4f0230; frame = (0 0; 768 43); opaque = NO; Gesturerecognizers = <NSArray:0xc4f9770>; Layer = <CALayer:0xc4ee090>>
(LLDB) PO [[TextView Superview] Superview]
<UITableViewCellScrollView:0xc4f9150; frame = (0 0; 768 44); Clipstobounds = YES; AutoResize = w+h; Gesturerecognizers = <NSArray:0xc4f93b0>; Layer = <CALayer:0xc4f9320>; Contentoffset: {0, 0}>
(LLDB) PO [[[[TextView Superview] superview] Superview]
<ThreeElementsCell:0xc4eeab0; BaseClass = UITableViewCell; frame = (0 296; 768 44); AutoResize = W; Layer = <CALayer:0xc4ee060>>
(LLDB) po [cell Superview]
<UITableViewWrapperView:0xb5b8ea0; frame = (0 0; 768 964); AutoResize = w+h; Layer = <CALayer:0xb5b8f10>>
(LLDB) PO [[Cell Superview] Superview]
<TPKeyboardAvoidingTableView:0xcc5aa00; BaseClass = UITableView; frame = (0 0; 768 964); Clipstobounds = YES; AutoResize = LM; Gesturerecognizers = <NSArray:0xb5b7880>; Layer = <CALayer:0xb5b6e80>; Contentoffset: {0, -64}>
(LLDB)
Parsing: TextView is to use Xib directly dragged a uitextview placed in the UITableViewCell of a control, I set a breakpoint after the above code, printing as shown above, see, It is not difficult to conclude that the structure of Ios7uitableview iOS7 The following version of the change, IOS7 previous version is uitalbeview directly under Uitableviewcell,uitableviewcell is Contentview, Xib drag and drop the control is placed directly on the Contentview above, and iOS7 above the version is Uitalbeview has a subview-uitableviewwrapperview, Uitableviewwrapperview's Subview is Uitableviewcell,uitableviewcell a subview is Uitableviewcellscrollview, Uitableviewcellscrollview Subview is contentview,contentview above drag and drop is you put on the control, so in iOS version adaptation when the corresponding version of the judgment, and processing.
Reference Link: 1.http://blog.csdn.net/software_notes/article/details/182628132.http://blog.csdn.net/chenyblog/article/ details/44490659
Get all the hierarchy related to UIView in iOS