如何循环遍历 UIView 的所有子视图及其子视图和子视图
如何循环遍历 UIView 的所有子视图及其子视图和子视图?
How can I loop through all subviews of a UIView, and their subviews and their subviews?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
使用递归:
Use recursion:
这是我使用递归和 UIView 类的包装器(类别/扩展)的解决方案。
用法:现在您应该循环遍历所有子视图并根据需要操作它们。
Well here is my solution using recursion and a wrapper(category/extension) for the UIView class.
Usage : Now you should be looping through all the sub views and manipulate them as needed.
这是另一个 Swift 实现:
Here's another Swift implementation:
Swift 3 中的解决方案提供所有
子视图
,而不包含视图本身:A solution in Swift 3 that gives all
subviews
without including the view itself:我在创建所有内容时对其进行标记。然后很容易找到任何子视图。
I tag everything when it's created. Then it's easy to find any subview.
刚刚找到了一种通过调试器执行此操作的有趣方法:
http:// /idevrecipes.com/2011/02/10/exploring-iphone-view-hierarchies/
引用了此 Apple 技术说明:
https://developer.apple.com/library/content/technotes/tn2239/_index.html#SECUIKIT
只需确保您的调试器已暂停(或者设置手动暂停的断点),您可以询问
recursiveDescription
。Just found an interesting way to do this through the debugger:
http://idevrecipes.com/2011/02/10/exploring-iphone-view-hierarchies/
references this Apple Technote:
https://developer.apple.com/library/content/technotes/tn2239/_index.html#SECUIKIT
Just make sure your debugger is paused (either set a break point of pause it manually) and you can ask for the
recursiveDescription
.这是一个具有实际视图循环和中断功能的示例。
Swift:
调用示例:
反向循环:
调用示例:
Objective-C:
调用示例:
Here is an example with actual view looping and breaking functionality.
Swift:
Call example:
Reversed looping:
Call example:
Objective-C:
Call example:
在奥勒·贝格曼的帮助下。我添加了几行来将块概念融入其中。
UIView+HierarchyLogging.h
UIView+HierarchyLogging.m
在 ViewController 中使用 HierarchyLogging 类别。您现在可以自由地做您需要做的事情。
With the help of Ole Begemann. I added a few lines to incorporate the block concept into it.
UIView+HierarchyLogging.h
UIView+HierarchyLogging.m
Using the HierarchyLogging category in your ViewController. You are now have freedom to what you need to do.
不需要创建任何新函数。用 Xcode 调试时就这样做。
在视图控制器中设置断点,并使应用程序在此断点处暂停。
右键单击空白区域,然后在 Xcode 的 Watch 窗口中按“添加表达式...”。
输入这一行:
如果值太长,右键单击它并选择“打印...的说明”。您将在控制台窗口中看到 self.view 的所有子视图。如果您不想看到 self.view 的子视图,请将 self->_view 更改为其他内容。
完毕!没有gdb!
Need not create any new function. Just do it when debugging with Xcode.
Set a breakpoint in a view controller, and make the app pause at this breakpoint.
Right click the empty area and press "Add Expression..." in Xcode's Watch window.
Input this line:
If the value is too long, right click it and choose "Print Description of ...". You will see all subviews of self.view in the console window. Change self->_view to something else if you don't want to see subviews of self.view.
Done! No gdb!
这是一个递归代码:-
Here is a recursive code:-
顺便说一句,我制作了一个开源项目来帮助完成此类任务。它非常简单,并使用 Objective-C 2.0 块在层次结构中的所有视图上执行代码。
https://github.com/egold/UIViewRecursion
示例:
By the way, I made an open source project to help with this sort of task. It's really easy, and uses Objective-C 2.0 blocks to execute code on all views in a hierarchy.
https://github.com/egold/UIViewRecursion
Example:
以下是上面 Ole Begemann 的答案的一个变体,它添加了缩进来说明层次结构:
Here is a variation on Ole Begemann's answer above, which adds indentation to illustrate the hierarchy:
这个答案中发布的代码遍历了所有窗口和所有视图及其所有子视图。它用于将视图层次结构的打印输出转储到 NSLog,但您可以使用它作为任何视图层次结构遍历的基础。它使用递归 C 函数来遍历视图树。
The code posted in this answer traverses all windows and all views and all of their subviews. It was used to dump a printout of the view hierarchy to NSLog but you can use it as a basis for any traversal of the view hierarchy. It uses a recursive C function to traverse the view tree.
我写了一个类别一段时间来调试一些视图。
IIRC,发布的代码是有效的。如果没有,它会为您指明正确的方向。使用时需自担风险等。
I wrote a category some time back to debug some views.
IIRC, the posted code is the one that worked. If not, it will point you in the right direction. Use at own risk, etc.
这也显示了层次结构级别
This displays the hierarchy level as well
希望我先找到此页面,但如果(出于某种原因)你想非递归地执行此操作,则不是在一个类别中,并且有更多行代码
Wish I'd found this page first, but if (for some reason) you want to do this non-recursively, not in a Category, and with more lines of code
我认为所有使用递归的答案(调试器选项除外)都使用了类别。如果您不需要/想要类别,则可以仅使用实例方法。例如,如果您需要获取视图层次结构中所有标签的数组,您可以这样做。
I think all of the answers using recursion (except for the debugger option) used categories. If you don't need/want a category, you can just use a instance method. For instance, if you need to get an array of all labels in your view hierarchy, you could do this.
下面的方法创建一个或多个可变数组,然后循环访问输入视图的子视图。这样做时,它会添加初始子视图,然后查询该子视图是否有任何子视图。如果为真,它会再次调用自身。它将一直这样做,直到添加了层次结构的所有视图。
呼叫使用:
The method below creates one or more mutable arrays, then loops through the subviews of the input view. In doing so it adds the initial subview then queries as to whether there are any subviews of that subview. If true, it calls itself again. It does so until the all the views of the hierarchy have been added.
Call using: