如何列出iOS中uiviewcontroller中的所有子视图?
我想列出 UIViewController 中的所有子视图。我尝试了 self.view.subviews ,但并没有列出所有子视图,例如没有找到 UITableViewCell 中的子视图。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
您必须递归迭代子视图。
You have to recursively iterate the sub views.
xcode/gdb 转储视图层次结构的内置方法很有用——recursiveDescription,根据 http://developer.apple.com/library/ios/#technotes/tn2239/_index.html
它输出一个更完整的视图层次结构,您可能会发现它有用:
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:
Swift 中优雅的递归解决方案:
您可以在任何 UIView 上调用 subviewsRecursive() :
Elegant recursive solution in Swift:
You can call subviewsRecursive() on any UIView:
您需要递归打印,此方法还根据视图的深度进行制表符
You need to print recursively, this method also tabs based on the depth of the view
这是快速版本
Here is the swift version
详细信息
解决方案
使用
结果
Details
Solution
Usage
Result
我参加聚会有点晚了,但有一个更通用的解决方案:
I'm a bit late to the party, but a bit more general solution:
如果您想要的只是一组
UIView
,这是一个单行解决方案 (Swift 4+):If all you want is an array of
UIView
s, this is a one liner solution (Swift 4+):我使用这种方式:
在 UIViewController 中。
I use this way:
in the UIViewController.
以我的方式,UIView的类别或扩展比其他的好得多
而递归是让所有子视图了解更多信息的关键点
:
https://github.com/ZhipingYang/XYDebugView
Objective-C
示例
Swift 3.1
示例
直接使用sequence函数获取所有子视图
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
简单的 Swift 示例:
Simple Swift example:
UITableViewCell 中的子视图未打印的原因是您必须输出顶层的所有子视图。单元格的子视图不是视图的直接子视图。
为了获取 UITableViewCell 的子视图,您需要在打印循环中确定哪些子视图属于 UITableViewCell (使用
isKindOfClass:
),然后循环遍历其子视图编辑:这篇关于 简单的 UIView 调试 可能会有所帮助
The reason the subviews in a UITableViewCell are not printed is because you must be outputting all the subviews in the top level. The subviews of the cell are not the direct subviews of your view.
In order to get the UITableViewCell's subviews, you need to determine the which subviews belong to a UITableViewCell (using
isKindOfClass:
) in your print loop and then loop through it's subviewsEdit: This blog post on Easy UIView Debugging may potentially help
我编写了一个类别来列出视图控制器持有的所有视图,其灵感来自之前发布的答案。
要列出视图控制器持有的所有视图,只需
NSLog("%@",[self listOfSubviews])
,其中self
表示视图控制器本身。虽然它的效率并不高。另外,您可以使用 NSLog(@"\n%@", [(id)self.view PerformSelector:@selector(recursiveDescription)]); 来做同样的事情,我认为它更多比我的实施效率高。
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.您可以尝试一种奇特的数组技巧,例如:
[self.view.subviews makeObjectsPerformSelector: @selector(printAllChildrenOfView)];
只需一行代码。当然,您可能需要调整您的方法
printAllChildrenOfView
以不接受任何参数或创建新方法。You could try a fancy array trick, like:
[self.view.subviews makeObjectsPerformSelector: @selector(printAllChildrenOfView)];
Just one line of code. Of course, you might need to adjust your method
printAllChildrenOfView
to not take any parameters or make a new method.兼容 Swift 2.0
这里有一个递归方法来获取通用视图的所有子视图:
所以你可以用这种方式在任何地方调用:
Swift 2.0 compatible
Here a recursive method to obtains all subviews of a generic view:
So you can call everywhere in this way:
最短解决方案
结果
注释
Shortest solution
Result
Notes
这是对这个答案的重写:
您必须首先获取指向要打印其所有子视图的对象的指针/引用。有时您可能会发现通过其子视图访问该对象更容易找到该对象。就像
po someSubview.superview
。这将为您提供如下信息:要打印 superView,您必须使用断点。
现在要执行此步骤,您只需单击“查看调试层次结构”即可。不需要断点
然后你可以轻松地做到:
这对我来说返回了类似的内容:
你一定已经猜到我的超级视图有 4 个子视图:
这对我来说相当新,但帮助我调试了我的视图的框架(以及文本和类型)。我的子视图之一没有显示在屏幕上,因此使用了 recursiveDescription,我意识到我的子视图之一的宽度是
0
...所以我纠正了它的约束,并且子视图出现了。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.或者,如果您想从 UIView 扩展返回所有子视图(和嵌套子视图)的数组:
Alternatively if you wanted to return an array of all subviews (and nested subviews) from a UIView Extension:
AC# Xamarin 版本:
或者,如果您想查找我使用的特定类型的所有子视图:
A C# Xamarin version:
Alternatively, if you want to find all the subviews of a specific type I use:
我已经在 UIView 的类别中完成了它,只需调用传递索引的函数即可以良好的树格式打印它们。这只是 James Webster 发布的答案的另一个选项。
我希望它有帮助:)
I have done it in a category of
UIView
just call the function passing the index to print them with a nice tree format. This is just another option of the answer posted by James Webster.I hope it helps :)
self.view.subviews 维护视图的层次结构。要获取 uitableviewcell 的子视图,您必须执行如下操作。
self.view.subviews maintain the heirarchy of views.To get subviews of uitableviewcell you have to do something like below.