如何引用“最顶层”看法?

发布于 2024-12-13 15:59:30 字数 182 浏览 0 评论 0原文

iOS中,以编程方式,如何找到topmostUIView是什么?

换句话说..现在显示什么视图?

假设我有一个笔尖,其中有 3 个视图堆叠在一起。在程序中,如果我知道顶视图是什么,我可以删除它。我怎样才能知道笔尖顶部的视图是什么?

In iOS, programmatically, how can one find what top most UIView is?

In other words .. what view is displayed right now?

Say, i have a nib with 3 views stacked on top of one another. Inside a program, i can remove the top view if I know what it is. How can i find out what view is on top of the nib?

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

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

发布评论

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

评论(3

云朵有点甜 2024-12-20 15:59:30

您可以拥有许多最顶层的视图,因为视图不必占据整个屏幕。

如果您想要视图的最上面的子视图,您可以调用

[yourView subviews];

并获取最后一个(它们按显示顺序,最前面的最后一个)

[[yourView subviews] objectAtIndex:[[yourView subviews] count]];

编辑:这更好(来自注释)

[[yourView subviews] lastObject];

如果您知道您显示的 viewController 只需将 yourView 替换为 yourController 。看法

You can have a lot of top most views because a view doesn't have to take all the screen.

If you want the top most subview of a view, you can call

[yourView subviews];

and take tje last one (they are in displayed order, most front last)

[[yourView subviews] objectAtIndex:[[yourView subviews] count]];

edit: this is better (from comments)

[[yourView subviews] lastObject];

If you know your displayed viewController just replace yourView by yourController.view

灵芸 2024-12-20 15:59:30

像这样

UIView *topMost = [[self.view subviews] lastObject];

Like this

UIView *topMost = [[self.view subviews] lastObject];
柳若烟 2024-12-20 15:59:30

或者像这样的

UIWindow *window = [[UIApplication sharedApplication].keyWindow;
UIView *topMost = [window findTopMostViewForPoint:CGPointMake(100, 100)];

功能

@implementation UIView (Extra)

- (UIView *)findTopMostViewForPoint:(CGPoint)point
{
    for(int i = self.subviews.count - 1; i >= 0; i--)
    {
        UIView *subview = [self.subviews objectAtIndex:i];
        if(!subview.hidden && CGRectContainsPoint(subview.frame, point))
        {
            CGPoint pointConverted = [self convertPoint:point toView:subview];
            return [subview findTopMostViewForPoint:pointConverted];
        }
    }

    return self;
}

- (BOOL)isTopmostViewInWindow
{
    if(self.window == nil)
    {
        return NO;
    }

    CGPoint centerPointInSelf = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGPoint centerPointOfSelfInWindow = [self convertPoint:centerPointInSelf toView:self.window];
    UIView *view = [self.window findTopMostViewForPoint:centerPointOfSelfInWindow];
    BOOL isTopMost = view == self || [view isDescendantOfView:self];
    return isTopMost;
}

@end

Or like this

UIWindow *window = [[UIApplication sharedApplication].keyWindow;
UIView *topMost = [window findTopMostViewForPoint:CGPointMake(100, 100)];

Functions

@implementation UIView (Extra)

- (UIView *)findTopMostViewForPoint:(CGPoint)point
{
    for(int i = self.subviews.count - 1; i >= 0; i--)
    {
        UIView *subview = [self.subviews objectAtIndex:i];
        if(!subview.hidden && CGRectContainsPoint(subview.frame, point))
        {
            CGPoint pointConverted = [self convertPoint:point toView:subview];
            return [subview findTopMostViewForPoint:pointConverted];
        }
    }

    return self;
}

- (BOOL)isTopmostViewInWindow
{
    if(self.window == nil)
    {
        return NO;
    }

    CGPoint centerPointInSelf = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGPoint centerPointOfSelfInWindow = [self convertPoint:centerPointInSelf toView:self.window];
    UIView *view = [self.window findTopMostViewForPoint:centerPointOfSelfInWindow];
    BOOL isTopMost = view == self || [view isDescendantOfView:self];
    return isTopMost;
}

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