Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of setting some kind of z-index for z-position of UIImageView objects?

I'm building up a grid of images, and I start at the top, creating UIImageView objects all over the place. Now I want that they overlap a little bit, but the last ones I draw are the ones most on top. I'd like to set the z-position programmatically, so that the first ones I draw will be the ones most on top.

like image 688
Thanks Avatar asked May 05 '09 17:05

Thanks


People also ask

What is Z index in Swift?

Discussion. This property is used to determine the front-to-back ordering of items during layout. Items with higher index values appear on top of items with lower values. Items with the same value have an undetermined order. The default value of this property is 0.


9 Answers

imageView.layer.zPosition = 100;
like image 94
Black Tiger Avatar answered Nov 10 '22 04:11

Black Tiger


instead of using insertSubiew you can try:

- (void)bringSubviewToFront:(UIView *)view
- (void)sendSubviewToBack:(UIView *)view
like image 40
Patryk Peszko Avatar answered Nov 10 '22 02:11

Patryk Peszko


You said you used UIImageView. Drawing an image with UIImageView means initializing it and then adding it to a container view.

Can't you just use this code in your loop?

[containerView insertSubview:image atIndex:[[containerView subviews] count]]

The result would be that the view added first is on the top of the view stack (I haven't been able to test this yet though :) )

like image 32
ullmark Avatar answered Nov 10 '22 04:11

ullmark


https://stackoverflow.com/a/4631895/2482283

You can use the zPosition property of the view's layer (it's a CALayer object) to change the z-index of the view.

theView.layer.zPosition = 1;

As Viktor Nordling added, "big values are on top. You can use any values you want, including negative values." The default value is 0.

You need to import the QuartzCore framework to access the layer. Just add this line of code at the top of your implementation file.

#import "QuartzCore/QuartzCore.h"

another way(https://stackoverflow.com/a/4663526/2482283), you can also utilize uiviews method:

@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;
like image 22
lbsweek Avatar answered Nov 10 '22 02:11

lbsweek


Not directly.

One solution would be giving the UIViews a z-index property; an integer defining your depth level. Then you could itterate through an NSArray (myArray) containing the UIViews adding them as subviews in order of depth.

like image 31
Kriem Avatar answered Nov 10 '22 02:11

Kriem


I had the same problem. I needed the subview element with the highest and lowest z-index. From my testing I found out, that UIKit arranges the elements in the subviews-property according to their z-position. If you take [self.scrollview subviews] for example. The first array element is the subview which is at the very back. The array element with the highest index is the subview at the very front. If you use

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

the subviews array will be reordered accordingly.

like image 21
Matt Avatar answered Nov 10 '22 04:11

Matt


If you have the ref to the view below/above you need to add the sub view you can use the following

[self.view insertSubview:<#(UIView *)#> belowSubview:<#(UIView *)#>];
[self.view insertSubview:<#(UIView *)#> aboveSubview:<#(UIView *)#>];
like image 27
Pete Avatar answered Nov 10 '22 02:11

Pete


Actually, as far as I know, there is no z-index position property for that. Drawing in the view uses a simple drawing model.

You can either put your logic in the drawing loop (i.e draw the things you want on top last) or you can use Cocoa Animation, since unlike UIImageView, CALayers do have depth (they are a 2D projection of a 3D space). See here for a reference.

like image 28
Hejazzman Avatar answered Nov 10 '22 04:11

Hejazzman


You really might want to consider using CALayer's instead of lots of UIImageView Objects. UIImageView objects are great but for intense drawing CALayer's are lighter weight and more efficient (though if you need to have a user interact with the images, a view object is better). It really depends on what you are doing. CALayers do have a zPosition property. This is what UIKit uses when it arranges the UIView's position in the view stack.

like image 43
Jay Avatar answered Nov 10 '22 02:11

Jay