gpt4 book ai didi

ios - 在 ViewController 中获取所有 View

转载 作者:行者123 更新时间:2023-11-28 12:14:02 25 4
gpt4 key购买 nike

我正在尝试获取我的 UIViewController 中的所有 View 。

我正在使用这段代码:

    for subview : UIView in self.view.subviews{
//code here
}

问题是我没有获得 UIView 内的 View 。所以除了他们的 child ,我能看到所有的人。

请问,如何在我的 UIViewController 中获取所有 subview 或父 View ?即使一个 View 有一个 child 有一个 child 有一个 child 。我想遍历所有 View 。

提前致谢:)

(请使用 swift 4.0)

最佳答案

您需要递归处理所有 subview 。

func processSubviews(of view: UIView) {
// 1. code here do something with view
for subview in view.subviews {
// 2. code here do something with subview
processSubviews(of: subview)
// 3. code here do something with subview
}
// 4. code here do something with view
}

根据您想要的结果,您需要将代码放在位置 1、2、3 或 4(或者可能是这些位置中的两个或更多位置)。

然后你可以这样调用它:

processSubviews(of: self.view)

关于ios - 在 ViewController 中获取所有 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47463865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com