如何获取UIView层次结构索引? (即其他子视图之间的深度) [英] How to get UIView hierarchy index? (i.e. the depth in between the other subviews)

查看:260
本文介绍了如何获取UIView层次结构索引? (即其他子视图之间的深度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从UIView文档:

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

我可以在特定索引处插入UIView很棒,但是我找不到一种方法来读取给定UIView所具有的索引.

It's great that I can insert a UIView at a certain index, but I cannot find a way to READ what index a given UIView has.

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

I need to check whether the UIView is on top, or at the back ...

既然Brandon指出了子视图数组中的顺序,我将免费提供了一个UIView类别以供下载,该类别提供了一些方便的方法来处理子视图层次结构: http://www.touch-code- magazine.com/uiview-layout-hierarchy-uiview-category-to-download/

Now that Brandon pointed out the order in the subviews array, I put together a UIView Category free for download providing few handy methods to handle the subviews hierarchy here: http://www.touch-code-magazine.com/uiview-layout-hierarchy-uiview-category-to-download/

推荐答案

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

I am almost 100% sure that the index is the same as the index of the subView inside the superViews subviews property.

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

我刚刚使用以下代码对其进行了测试,并且可以正常工作

I just tested this with the following code and it works

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

这篇关于如何获取UIView层次结构索引? (即其他子视图之间的深度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆