gpt4 book ai didi

ios - 如何获取最外层父 UIView 容器?

转载 作者:搜寻专家 更新时间:2023-10-30 23:15:19 25 4
gpt4 key购买 nike

如何获得当前 self UIView 的父级 View ,目前我正在使用 self.superview?.superview?.superview?。 superview?.superview?.superview?

它看起来有点奇怪,不是使用上面的语法而是他们获取主 UIView 的任何其他方式,否则我怎么能阻止这种与 guard 的可选链接?

最佳答案

您可以将 guard 与返回可选的方法一起使用(我假设您是从 UIView 自定义类调用它)。

private func outerMostParent(view:UIView)-> UIView? {
guard let parent = superview?.superview?.superview?.superview?.superview?.superview
else{
return nil
}
return parent
}

然后你可以做

self.outerMostParent(self)?.addSubview(yourSubview)

关于ios - 如何获取最外层父 UIView 容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328845/

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