简体   繁体   English

如何找到最父级的SuperView?

[英]How do I find the most parent SuperView?

I'm trying to add a new subView to a page, such that everything but itself is greyed out. 我正在尝试向页面添加新的subView,以便除自身以外的所有内容都显示为灰色。 However, I'm calling it from within a subview of the screen. 但是,我是在屏幕的子视图中调用它的。 To get it out, I have to do the following : 为了解决这个问题,我必须执行以下操作:

[self.view.superview.superview.superview.superview addSubview:self.cardDialog.view];

As you can surmise, this is extremely bad code. 可以推测,这是非常糟糕的代码。 How can I find the proper parent level and set it correctly? 如何找到合适的父级并正确设置?

If the view is part of the view hierarchy, use the window property. 如果视图是视图层次结构的一部分,请使用window属性。

UIView* topView = self.view.window;

Or if your view is not on screen yet, you can get the window indirectly through your app delegate 或者,如果您的视图尚未显示在屏幕上,则可以通过应用程序委托间接获取窗口

UIView* topView = [UIApplication sharedApplication].delegate.window;

Else, if your target is not the window, you can walk up the view hierarchy until you find the view you want: 否则,如果您的目标不是窗口,则可以遍历视图层次结构,直到找到所需的视图:

UIView* topView = self.view;

while(topView.superview != nil){
    topview = topView.superview;

    if( /*topview is the one you were looking for*/ ){
        break;
    }
}

I've found myself on this situation once and I gave up. 我曾经遇到过这种情况,所以我放弃了。 I decided to use a modalView instead with a cross disolve transition. 我决定使用modalView代替交叉溶解过渡。 Not only you avoid the problem you are having but it helps keeping things organized. 您不仅可以避免遇到的问题,而且还可以使事情井井有条。 Lately I improved this aproach by grabing a snapshot of the parentVC and sending it to the modalView, then I apply a tint. 最近,我通过获取parentVC的快照并将其发送到modalView来改进了此方法,然后应用了色调。 The overall effect is exactly what you are looking for, I guess. 我想总体效果就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM