如何在iPhone App中抢到最高级别的Superview?

发布于 2024-10-04 07:58:47 字数 255 浏览 0 评论 0原文

我希望能够获取应用程序的主 UIView。到目前为止,我一直在调用 [[[self.view superview] superview] superview] 来向上遍历链(或向下,取决于你如何看待它)。问题是我可能无法绝对确定我需要升多少级。有没有办法:

  1. 直接进入 iPhone 应用程序中的最高级别 UIView?

或者,如果不是,

  1. 检查当前超级视图是否是应用程序中最高级别的 UIView?

I want to be able to grab the main UIView for the application. So far, I have been calling [[[self.view superview] superview] superview] to traverse up the chain (or down, depending on how you look at it). The problem with this is that it is possible that I can't be absolutely sure the number of levels up I need to go. Is there a way to:

  1. Go directly to the highest level UIView in an iPhone application?

or, if not,

  1. Check to see if the current superview is the highest level UIView in the app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

源来凯始玺欢你 2024-10-11 07:58:47

我不确定这是否是您首先要寻找的,但它可以提供帮助。

如果您试图找到顶部视图以添加子视图(例如弹出窗口或其他视图),您可以使用 UIWindow,它在某种程度上是顶级视图:

UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:yourView];

I'm not sure that it's what your are looking for in the first place, but it can help.

If you are trying to find the top view in order to add a subview, like say a popup or other, you can use UIWindow, which is in some way, the top level view:

UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:yourView];
最终幸福 2024-10-11 07:58:47

只是将其从顶部拉下来,例如检查当前视图的 superview 属性是否为空?如果是的话,我想我们可以假设它是根视图。

Just pulling this off the top, something like checking if the superview property of the current view is null? If it is I guess we may presume that it is the root view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文