Positioning UIView for iPhone: Any Z-Index Concept?

I know if I want to add a black background to the UIImageView, I can create a UIImageView that is slightly smaller than the UIView it contains. If I do this, the UIImageView will be located on top of the black background.

Just hypothetically speaking, suppose I need a black UIView background at the top of the UIImageView (so that it covers the image), how do I do this? Is there a concept of z-index?

Here is my code to add a black background to the image:

UIView *blackBG = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,100)]; blackBG.backgroundColor = [UIColor blackColor]; UIImageView *myPicture = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"myPicture.jpg"]]; int borderWidth = 10; myPicture.frame = CGRectMake(borderWidth, borderWidth, blackBG.frame.size.width-borderWidth*2, blackBG.frame.size.height-borderWidth*2)]; [blackBG addSubview: myPicture]; 
+4
source share
1 answer

Subviews are multi-level, you can send subview back using ...

 [myView sendSubviewToBack:mySubView]; 

or bringSubviewToFront etc.

You can also make one of them ...

 [myView insertSubview:mySubview atIndex:0]; //Places it at the bottom [myView insertSubview:mySubview belowSubview:anotherSubview]; //Places it below anotherSubview 

Once you add a view as a view to another view, however the parent view is the bottom of the stack. You can play with CALayer z orders, but you bypass the general approach to representations and representations.

Create a single view as a parent for both the bg view and the image, then you can order them as you like.

UPDATE: I have a UIView + Layout category which has nice [subview sendToBack]; and [subview bringToFront]; that may be useful .... here

+28
source

All Articles