How can I get the sub UIView index in the parent view?

I want to use exchangeSubviewAtIndex: with the SubviewAtIndex: method.

but how can I get the indices of these views? I already have pointers, so I thought there might be such a method.

[parentView indexOfSubview:subview]; 

but I could not find one in the Xcode document library.

Please help me! thanks in advance.

+4
source share
4 answers

Here's how you get it:

[parentView.subviews indexOfObject:subview];

+16
source

You can find out the index of your object as you find in NSArray , since 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 the UIView hierarchy index ??? (i.e. depth between other areas)

+6
source
 [View.subviews indexOfObject:SubView]; 
+2
source

You can get the index of the array using this.

 [view.subViews indexOfObject:yourSubView]; 

And try to catch this index.

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

Try this code, maybe you will find it useful ...

+1
source

Source: https://habr.com/ru/post/1492427/


All Articles