0

I have a view between other views, the order may vary. For example: viewA on top of viewB, on top of viewC, or in another order.

Suppose I have

viewA, viewB, viewC, viewD and viewE

A is on the top and E on the bottom.

I need to replace viewC with viewZ, but I need to insert viewZ in the same index of viewC.

How do I know, before removing viewC, what index it has, so I can insert viewZ using

[self.view insertSubview:viewZ atIndex:?????)

and it will be at the same level?

thanks for any help.

Duck
  • 34,902
  • 47
  • 248
  • 470

2 Answers2

1

From How to get UIView hierarchy index ??? (i.e. the depth in between the other subviews)

int index = [[superView subviews] indexOfObject:viewC];
Community
  • 1
  • 1
huggie
  • 17,587
  • 27
  • 82
  • 139
1

Besides getting the index, you could also use -insertSubview:aboveSubview: (and …belowSubview:):

UIView* superview = self.view;
[superview insertSubview:viewZ aboveSubview:viewC];
[viewC removeFromSuperview];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005