4

If i use a for loop;

for(UIView *subview in [myView subviews])

in which order, subviews are brought?

According to their added order or something else?

erdemgc
  • 1,701
  • 3
  • 23
  • 43
  • 5
    Something else. You just need to fire up the [official documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW17) and you'll easily find it out. –  Sep 05 '13 at 12:24
  • but you can also check UIClass such like if([subView isKindOfClass:[UILabel class]]) // Check is SubView Class Is UILabel class? { // You can write code here for your UILabel; } – iPatel Sep 05 '13 at 12:26
  • 5
    @iPatel I don't see how that's relevant to the question. –  Sep 05 '13 at 12:26
  • @H2CO3 - yup but i just want to inform to OP that you can also get it like this, so.. i putted it as comment also :) thanks :) – iPatel Sep 05 '13 at 12:31
  • 1
    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. this is what says subviews method description. – Suryakant Sharma Sep 05 '13 at 12:33

2 Answers2

8

As the documentation says, it reflects their visible order in the screen.

@property(nonatomic, readonly, copy) NSArray *subviews

Discussion

You can use this property to retrieve the subviews associated with your custom view hierarchies. 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.

For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time. Therefore, you should not attempt to retrieve or modify subviews for these types of system-supplied views. If you do, your code may break during a future system update.

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • 1
    I'm probably not really good in English, but what is "visible order"? How does it count? What is back-most? Back-most from which side? Does it mean the order the subviews were added to their superview? – Andrey Chernukha Jan 06 '16 at 16:51
  • 2
    Old question and comment, but @AndreyChernukha, back meaning the farthest view from the viewer, the bottom of the stack in a 3D hierarchy. When you add views, they get added to the top of the stack, so the order is almost the same as added order, except for explicit re-orderings. – Chris Conover Feb 08 '18 at 06:56
  • 2
    @ChrisConover Thanks a ton, buddy. That makes sense – Andrey Chernukha Feb 08 '18 at 12:22
1

I find it to be useful, and in my experience it places subviews in order that they were added. Just be careful using them when using UIScrollView. I occasionally ran into 'phantom' subviews that are created for scroll bars

Murat Zazi
  • 347
  • 4
  • 10