0

Is there a way to change the "layer" that UIImageView objects are drawn in? Whenever I add an image view to a view controller it defaults to drawing the most recently added one on top of all the others. So if I decide to add a "background" image it is now a "foreground" image and blocks everything else.

There isn't anything in the IB options or in the UIImageView class reference and I haven't been able to find anything here on SO. It's been a problem for a while and it's weird that I haven't seen anything about it before... I think it might just be my semantics coming from a delphi background.

Anyway, does anyone know about this issue / ICANHAZTEHCODEZ to fix it? Or is this just like the UIScrollView problem and poorly supported by the development environment.Y U NO WORK

This happens when I try to use the editor to arrange the subviews.

Dustin
  • 6,783
  • 4
  • 36
  • 53

2 Answers2

2

You can bring a SubView to Front or Send it background programmatically using

[self.view bringSubviewToFront:yourImageView];

and

[self.view sendSubviewToBack:yourImageView];

when self.view must be the superview of your imageView

In IB, select a UIControl and from top menu bar select Editor->arrage->send to front or back

atastrophic
  • 3,153
  • 3
  • 31
  • 50
  • Thanks for the tip... for some reason my send options are grayed out. I might have to add the images to the header file and then call `bringSubviewToFront`. – Dustin Aug 15 '12 at 16:26
  • You have to select the View in your IB first. Otherwise they appear greyed out. – atastrophic Aug 15 '12 at 16:28
  • Yeah, I selected it but when I looked up the help docs it says the new version of xcode does this randomly sometimes. Anyway, managed to fix it with your help and http://stackoverflow.com/questions/3378234/xcode-4-how-to-send-objects-in-nib-files-to-front-back – Dustin Aug 15 '12 at 16:29
  • there is a simple work around. As you can see there is a list of all views on left side where your imageView is selected. You can always drag those controls up and down thus changing their z-index. Dragging up means moving back, dragging down means sending forward. – atastrophic Aug 15 '12 at 16:36
  • Yep, that's what I did. Works great. – Dustin Aug 15 '12 at 16:37
1

When you use UIView's addSubview: method, it will add it to the top of the view stack resulting in what you are seeing.

There are numerous other UIView methods you can use to determine the order of subviews. E.g.:

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

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;

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

- (void)bringSubviewToFront:(UIView *)view;
Ian L
  • 5,553
  • 1
  • 22
  • 37