如何检查 UIView 是否是父视图的子视图

发布于 2024-12-10 18:33:17 字数 224 浏览 0 评论 0原文

我有一个应用程序,我添加了一个子视图(并根据用户交互删除相同的子视图)。我正在寻找一种方法来检查子视图在任何给定时间是否存在。

例如:

在当前视图 (UIView *viewA) 中,我添加一个子视图 (UIView *viewB)。然后我想要一种方法来检查 viewB 是否在任何给定时间显示。

抱歉,如果这不是很清楚,它很难描述。

I have an app which I add a subview to (and remove the same subview based on user interactions). I am looking for a way to check whether the subview is present or not at any given time.

For example:

In the current view (UIView *viewA) I add a subview (UIView *viewB). I then want a way of checking whether viewB is being displayed at any given time.

Sorry if this isn't very clear, it's quite hard to describe.

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

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

发布评论

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

评论(4

起风了 2024-12-17 18:33:17

UIView 存储其超级视图,并且可以通过 superview-property 只是尝试

if([viewB superview]!=nil)
    NSLog(@"visible");
else
    NSLog(@"not visible");

但更好的方法是使用 隐藏UIView 的 code>-property

an UIView stores its superview and is accessible with the superview-property just try

if([viewB superview]!=nil)
    NSLog(@"visible");
else
    NSLog(@"not visible");

But the better approach is to use the hidden-property of UIView

风向决定发型 2024-12-17 18:33:17

我遇到了同样的问题并咨询了 Apple 文档 并提出了这个优雅的解决方案:

if([self.childView isDescendantOfView:self.parentView])
{
    // The childView is contained in the parentView.
}

I went through the same issue and consulted Apple Documentation and came up with this elegant solution:

if([self.childView isDescendantOfView:self.parentView])
{
    // The childView is contained in the parentView.
}
孤凫 2024-12-17 18:33:17

我更新到了 Swift4,非常感谢@Shinnyx 和@thomas。

if viewB.superview != nil{     
    print("visible")
}
else{
   print("not visible")
}

if selfView.isDescendant(of: self.parentView) {
    print("visible")
}
else{
    print("not visible")
}

func isDescendant(of: UIView)

返回一个布尔值,指示接收者是给定视图的子视图还是与该视图相同。

I updated to Swift4, Thanks a lot to @Shinnyx and @thomas.

if viewB.superview != nil{     
    print("visible")
}
else{
   print("not visible")
}

if selfView.isDescendant(of: self.parentView) {
    print("visible")
}
else{
    print("not visible")
}

func isDescendant(of: UIView)

Returns a Boolean value indicating whether the receiver is a subview of a given view or identical to that view.

风吹过旳痕迹 2024-12-17 18:33:17

这是我放入 appDelegate 中的方法,以便我可以从任何点显示整个子视图层次结构。

// useful debugging method - send it a view and it will log all subviews
// can be called from the debugger
- (void) viewAllSubviews:(UIView *) topView Indent:(NSString *) indent  {
    for (UIView * theView in [topView subviews]){
        NSLog(@"%@%@", indent, theView);
        if ([theView subviews] != nil)
            [self viewAllSubviews:theView Indent: [NSString stringWithFormat:@"%@ ",indent]];
    }
}

用一个只有一个字符的字符串来调用它,它会为你缩进。 (即 [appDelegate viewAllSubviews:self.view Indent:@" "];

我发现首先清除调试窗格很有用,然后从调试器中调用它,并将其复制到文本编辑器中,例如BBEdit 将显示缩进。

您可以使用主窗口的视图调用它并查看屏幕上的所有内容。

Here's a method I put in the appDelegate so that I can display the entire subview hierarchy from any point.

// useful debugging method - send it a view and it will log all subviews
// can be called from the debugger
- (void) viewAllSubviews:(UIView *) topView Indent:(NSString *) indent  {
    for (UIView * theView in [topView subviews]){
        NSLog(@"%@%@", indent, theView);
        if ([theView subviews] != nil)
            [self viewAllSubviews:theView Indent: [NSString stringWithFormat:@"%@ ",indent]];
    }
}

call it with a string with one character and it will indent for you. (i.e. [appDelegate viewAllSubviews:self.view Indent:@" "];)

I find it useful to clear the debug pane first, then call this from the debugger, and copy it into a text editor like BBEdit that will show the indents.

You can call it using the mainWindow's view and see everything on your screen.

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