【问题标题】:How to get outermost parent UIView container?如何获取最外层的父 UIView 容器?
【发布时间】:2016-03-23 13:58:37
【问题描述】:

如何获得当前self UIView 的祖父视图,目前我正在使用self.superview?.superview?.superview?.superview?.superview?.superview?

它看起来有点奇怪,而不是使用上述语法是他们获得主要UIView 的任何其他方式,否则我如何防止与guard 的这种可选链接?

【问题讨论】:

  • 我只想addSubview(mySubView) 作为所有屏幕的叠加层,当删除此子视图时,我希望我以前的视图保持不变(如表格视图滚动位置保持不变)

标签: ios swift uiview guard superview


【解决方案1】:

您可以将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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多