0

.

Hello,

In Interface builder it really easy to set the order of items, as in the layers by dragging them one above the other.

I am not being successful to replicate that in code.

What is the line I need to add to have a similar effect?

Cheers

jwknz
  • 6,598
  • 16
  • 72
  • 115

2 Answers2

2

To quote another post, there are many useful methods for this exact purpose:

@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;

- (void)removeFromSuperview;
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

- (void)addSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;

- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;

Their names are fairly self explanatory. Of particular interest to you might be insertSubview:AtIndex, insertSubview:belowSubview (or above), as well as the bringSubviewToFront and sendSubviewToBack. These will allow you to build your view without having to add them in any specific order.

Community
  • 1
  • 1
CrimsonDiego
  • 3,616
  • 1
  • 23
  • 26
0

Just subview them in reversed order (if you want it on top, subview last). There are also functions to insert subview before or after a reference subview.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162