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.
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.
imageView.layer.zPosition = 100;
instead of using insertSubiew you can try:
- (void)bringSubviewToFront:(UIView *)view
- (void)sendSubviewToBack:(UIView *)view
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 :) )
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;
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.
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.
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 *)#>];
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With