当前位置:  开发笔记 > IOS > 正文

如何获取UIView层次结构索引?(即其他子视图之间的深度)

如何解决《如何获取UIView层次结构索引?(即其他子视图之间的深度)》经验,为你挑选了2个好方法。

来自UIView文档:

(void)insertSubview:(UIView *)view atIndex:(NSInteger)index

我可以在某个索引处插入UIView,这很棒,但我找不到一种方法来读取给定UIView的索引.

我需要检查UIView是在顶部,还是在后面......

编辑:既然布兰登指出了子视图数组中的顺序,我将UIView类别免费下载,提供了一些方便的方法来处理子视图层次结构:http: //www.touch-code-magazine.com/uiview-布局层次结构的UIView类到下载/



1> Brandon Bodn..:

我几乎100%确定索引与superViews subviews属性中的subView索引相同.

UIView * superView = .... some view
UIView * subView = .... some other view
[superView insertSubview:subView atIndex:index];
int viewIndex = [[superView subviews] indexOfObject:subView];
// viewIndex and index should be the same

我刚刚使用以下代码测试了它,它可以工作

UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
UIView* view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[self.view insertSubview:view1 atIndex:1];
[self.view insertSubview:view2 atIndex:2];
[self.view insertSubview:view3 atIndex:3];

NSLog(@"%d", [[self.view subviews] indexOfObject:view1]); // Is 1
NSLog(@"%d", [[self.view subviews] indexOfObject:view2]); // Is 2
NSLog(@"%d", [[self.view subviews] indexOfObject:view3]); // Is 3

[self.view bringSubviewToFront:view1];
NSLog(@"%d", [[self.view subviews] indexOfObject:view1]); // Is end of array

[self.view sendSubviewToBack:view1];
NSLog(@"%d", [[self.view subviews] indexOfObject:view1]); // Is 0


哈哈.是的,他们非常模糊,这就是为什么在我做了一堆测试之前我不确定答案的原因.结果是索引0返回,索引(视图的数量-1)是前面的,并且数组保持正确的顺序.我没有看到支持这个的参考文档,所以我希望它在以后的SDK中不会改变
它在UIView文档中.从子视图方法的"讨论"部分,"数组中子视图的顺序反映了它们在屏幕上的可见顺序,索引0处的视图是最后面的视图." 所以不用担心它会很快改变.这是官方的.

2> chown..:

设置递归方法如下:

- (void)printViewHierarchy:(UIView *)viewNode depth:(NSUInteger)depth
{
    for (UIView *v in viewNode.subviews)
    {
        NSLog(@"%@%@", [@"" stringByPaddingToLength:depth withString:@"|-" startingAtIndex:0], [v description]);
        if ([v.subviews count])
            [self printViewHierarchy:v depth:(depth + 2)]; // + 2 to make the output look correct with the stringPadding
    }
}

然后用:

[self printViewHierarchy:self.view depth:0];

输出:

[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: >
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->

[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-|-|-|->
[Line: 472] -[iPadRootViewController printViewHierarchy:depth:]: |-|-|-|-

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有