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?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
Simple Swift example:
In my way, UIView's category or extension is much better than others and recursive is the key point to get all subviews
learn more:
https://github.com/ZhipingYang/XYDebugView
Objective-C
example
Swift 3.1
example
directly, use sequence function to get all subviews
This a rewriting of this answer:
You must first get the pointer/reference to the object you intend to print all its subviews. Sometimes you may find it easier to find that object by accessing it through its subview. Like
po someSubview.superview
. This will give you something like:superview
0x7f91747c71f0
is the pointer to the superview.To print the superView, you must use breakpoints.
Now to do this step you could just click on the 'view debug hierarchy'. No need for breakpoints
Then you could easily do:
which for me returned something like:
as you must have guessed my superview has 4 subviews:
This is fairly new to me, but has helped me debug a my views' frames (and text and type). One of my subviews wasn't showing up on the screen, so used recursiveDescription and I realized the width of my one of my subView's was
0
... so I went corrected its constraints and the subview was appearing.I wrote a category to list all views held by a view controller which inspired by the answers posted before.
To list all views held by a view controller, just
NSLog("%@",[self listOfSubviews])
, whichself
means the view controller itself. Though it's not quit efficient.Plus, you can use
NSLog(@"\n%@", [(id)self.view performSelector:@selector(recursiveDescription)]);
to do the same thing, and I think it's more efficient than my implementation.You have to recursively iterate the sub views.
As commented by @Greg Meletic, you can skip the COUNT CHECK LINE above.