以编程方式获取“Z” UIView 与其兄弟姐妹相比的位置

发布于 2024-09-26 19:53:57 字数 815 浏览 0 评论 0原文

我的类具有在层次结构中的某些级别插入视图的逻辑。

containerView = [[UIView alloc] init];
shadowView = [[UIView alloc] init];
contentView = [[UIView alloc init];
[containerView addSubview:shadowView];
[containerView insertSubview:contentView belowSubview:shadowView];

随后,它会翻转它们,使阴影视图位于内容视图下方而不是上方。

// "Flipping" their positions.
[containerView insertSubview:contentView aboveSubview:shadowView];

UIView 有一个 subviews 属性返回 NSArray,不幸的是该数组不反映视图堆栈顺序。

我想对视图与其同级视图的位置进行比较。我该怎么做?

这是一个测试示例。

- (void)testViewHierarchyFlipping {
   STAssertEquals(containerView, shadowView.superview, nil);
   STAssertEquals(containerView, contentView.superview, nil);
   // Test that shadowView is ABOVE contentView.
}

My class has logic for inserting views in certain levels in the hierarchy.

containerView = [[UIView alloc] init];
shadowView = [[UIView alloc] init];
contentView = [[UIView alloc init];
[containerView addSubview:shadowView];
[containerView insertSubview:contentView belowSubview:shadowView];

Later down the line, it flips them so the shadow view is below the content view instead of above it.

// "Flipping" their positions.
[containerView insertSubview:contentView aboveSubview:shadowView];

UIView has a subviews property returning an NSArray, unfortunately the array does not reflect the view stack ordering.

I want to unit test the placement of the view compared to it's siblings. How can I do that?

Here's an example of a test.

- (void)testViewHierarchyFlipping {
   STAssertEquals(containerView, shadowView.superview, nil);
   STAssertEquals(containerView, contentView.superview, nil);
   // Test that shadowView is ABOVE contentView.
}

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

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

发布评论

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

评论(1

淡莣 2024-10-03 19:53:57

您可以检查 containerView.subviews 属性来确定每个子视图的 Z 位置。

数组开头的项目比数组末尾的项目更深。

我相信这应该适用于您的测试,尽管我目前无法访问 Xcode 来验证它是否正确。

- (void)testViewHierarchyFlipping {
   STAssertEquals(containerView, shadowView.superview, nil);
   STAssertEquals(containerView, contentView.superview, nil);

   // Test that shadowView is ABOVE contentView.
   UIView *topView = (UIView *)[[containerView subviews] lastObject];
   UIView *bottomView = (UIView *)[[containerView subviews] objectAtIndex:0];
   STAssertEquals(shadowView, topView, nil);
   STAssertEquals(contentView, bottomView, nil);
}

You can inspect the containerView.subviews property to determine the Z position of each subview.

Items at the beginning of the array are deeper than items at the end of the array.

I believe this should work for your test, although I don't have access to Xcode to verify that it's correct at the moment.

- (void)testViewHierarchyFlipping {
   STAssertEquals(containerView, shadowView.superview, nil);
   STAssertEquals(containerView, contentView.superview, nil);

   // Test that shadowView is ABOVE contentView.
   UIView *topView = (UIView *)[[containerView subviews] lastObject];
   UIView *bottomView = (UIView *)[[containerView subviews] objectAtIndex:0];
   STAssertEquals(shadowView, topView, nil);
   STAssertEquals(contentView, bottomView, nil);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文