How can I get the index of sub UIView in the paren

2019-02-17 06:54发布

I want to use exchangeSubviewAtIndex: withSubviewAtIndex: method.

but how can i get the indexes of those views? i already have their pointers, so I thought there might be a method like.

[parentView indexOfSubview:subview];

but I couldn't find that kind of thing in Xcode documents library.

please help me! thank you in advance.

标签: ios uiview
4条回答
叼着烟拽天下
2楼-- · 2019-02-17 07:37
[View.subviews indexOfObject:SubView];
查看更多
贪生不怕死
3楼-- · 2019-02-17 07:45

You can find out the index of your object as you find in NSArray, because view.subviews return an array

NSUInteger index1=[parentView.subviews indexOfSubview:subview1];
NSUInteger index2=[parentView.subviews indexOfSubview:subview2];

[parentView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];

You can follow this link for more How to get UIView hierarchy index ??? (i.e. the depth in between the other subviews)

查看更多
Deceive 欺骗
4楼-- · 2019-02-17 07:46

You can get array index using this.

[view.subViews indexOfObject:yourSubView]; 

And try to catch that index.

NSInteger index=[view.subViews indexOfObject:yourSubView]; 

Try this code might be helpful to you...

查看更多
Explosion°爆炸
5楼-- · 2019-02-17 07:56

This is how you get it:

[parentView.subviews indexOfObject:subview];

查看更多
登录 后发表回答