10

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1922950
  • 101
  • 1
  • 1
  • 3
  • http://stackoverflow.com/questions/2343432/how-to-get-uiview-hierarchy-index-i-e-the-depth-in-between-the-other-subvi check this – Nitin Gohel Jul 20 '13 at 05:22

4 Answers4

27

This is how you get it:

[parentView.subviews indexOfObject:subview];

In Swift:

parentView.subviews.firstIndex(of: subview)
Tulleb
  • 8,919
  • 8
  • 27
  • 55
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
9

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)

Community
  • 1
  • 1
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • your code needs a bit of editing @Rajneesh... NSUInteger index1=[parentView.subviews indexOfObject:subview1]; NSUInteger index2=[parentView.subviews indexOfObject:subview2]; [parentView exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2]; You'll note the indexOfSubview changes to indexOfObject and exchangeObjectAtIndex changes to exchangeSubviewAtIndex on the parentView – amergin Apr 19 '14 at 13:47
3
[View.subviews indexOfObject:SubView];
Rohit
  • 577
  • 1
  • 3
  • 13
2

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...

Jitendra
  • 5,055
  • 2
  • 22
  • 42