4

I bring a control to front with the below code:

[self.view bringSubviewToFront:control];

How can I recognize between two controls; which one is in front and which one is in back

Jonathan.
  • 53,997
  • 54
  • 186
  • 290
SajjadZare
  • 2,487
  • 4
  • 38
  • 68

2 Answers2

4

You would check the order of the view in the superview's (of the controls, in your case self.view) subviews property.

The view at the index 0 in the superview's subview is the view at the very back, and then the view at index 1 will be on top of it, and the view at index 2 will be on top of the view at index, etc
(basically a view is on top of the views with a lesser index compared to it's own index)

NSInteger indexOfControl1 = [[self.view subviews] indexOfObject:control1];
NSInteger indexOfControl2 = [[self.view subviews] indexOfObject:control2];
if (indexOfControl1 > indexOfControl2) {
    //control1 is on top of control2
}
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
2

If those controls have the same parent view their z-order is defined by their index in subviews array:

subviews: The order of the subviews in the array reflects their visible order on the screen, with the view at index 0 being the back-most view.

So your steps will be:
1. Get controls
2. Get their indexes in parent's subviews array (using indexOfObject: method)
3. Control with bigger index is in front

Vladimir
  • 170,431
  • 36
  • 387
  • 313