How to set iPhone UIView z index?
UIView
siblings are stacked in the order in which they are added to their superview. The UIView
hierarchy methods and properties are there to manage view order. In UIView.h:
@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;
The sibling views are ordered back to front in the subviews
array. So the topmost view will be:
[parentView.subviews lastObject];
and bottom view will be:
[parentView.subviews objectAtIndex:0];
Like Kolin Krewinkel said, [parentView bringSubviewToFront:view]
will bring the view to the top, but this is only the case if the views are all siblings in the hierarchy.
How do I change the z index or stack order of UIView?
I would recommend looking in the UIView
documentation, where are several methods listed for the manipulation of the order of subviews:
bringSubviewToFront(_:)
sendSubviewToBack(_:)
removeFromSuperview()
insertSubview(_:atIndex:)
insertSubview(_:aboveSubview:)
insertSubview(_:belowSubview:)
exchangeSubviewAtIndex(_:withSubviewAtIndex:)
In your situation, you could try:
self.view.sendSubviewToBack(myGradientView)
// self is your view controller in this case.
Alternatively, because you created the myGradientView
in IB, you could change the view hierarchy to change which views appeared on top. Here's a picture to illustrate:
Hope that helps.
Is there a way of setting some kind of z-index for z-position of UIImageView objects?
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 :) )
UIView Positioning for iPhone: Any concept of Z-Index?
Subviews are layered, you can send a subview to the back with...
[myView sendSubviewToBack:mySubView];
or bringSubviewToFront etc.
You can also do one of these...
[myView insertSubview:mySubview atIndex:0]; //Places it at the bottom
[myView insertSubview:mySubview belowSubview:anotherSubview]; //Places it below anotherSubview
As soon as you add a view as a subview to another view however, the parent view is the bottom of the stack. You can play around with CALayer z orders but you are bypassing the general approach of views and subviews.
Create a single view as a parent for both the bg view and the imageview, then you can order them how you like.
UPDATE: I have a UIView+Layout category which has nice [subview sendToBack];
and [subview bringToFront];
which you may find useful.... article here
place button over image (level / z-index)
You can use the following methods to organise the UIView programmatically:
bringSubviewToFront:
sendSubviewToBack:
insertSubview:aboveSubview:
insertSubview:belowSubview:
insertSubview:atIndex:
exchangeSubviewAtIndex:withSubviewAtIndex:
Here you can read up on the above.
However if you're creating the views with storyboards or in an xib then reordering the items will give you the desired effect.
Here the username UIView is behind the email UIView.
Another way of ordering is mentioned by PudiPudi, you can click the item and change it through the menus.
Another solution is the ordering at which you're adding the views programmatically. Simply changing the order of the, for example, addSubView() function will give a different layering.
UIView z-index transparency
My problem turned out to be completely unrelated. I instantiated the view from xib twice and for some reason this caused my issue.
Z Index of image and separator in UITableViewCell
You can change UIView
layer z-order with using setZPosition
. It is applied to all objects inherited from UIVIew
. May be is it a bug, bringSubviewToFront
not working for all cases as it suppose to.
[yourView.layer setZPosition:10];
Higher the number higher priority.
ios change subview index runtime
sendSubviewToBack:
and bringSubviewToFront:
are the methods you are looking for. Another possibility is exchangeSubviewAtIndex:withSubviewAtIndex:
if you want to exchange the layer of two views.
Related Topics
Fatal Error: Use of Unimplemented Initializer 'Init(Coder:)' For Class
Proper Use of Beginbackgroundtaskwithexpirationhandler
Getting a List of Files in a Directory With a Glob
Uibutton: Making the Hit Area Larger Than the Default Hit Area
Uinavigationbar Custom Back Button Without Title
Detecting Color of Iphone/Ipad/Ipod Touch
Core Data and iOS 7: Different Behavior of Persistent Store
How to Downgrade or Install an Older Version of Cocoapods
How to Compress of Reduce the Size of an Image Before Uploading to Parse as Pffile? (Swift)
How to Know When Uitableview Did Scroll to Bottom in Iphone
Looping a Video With Avfoundation Avplayer
How to Share an Image on Instagram in Ios
Uicollectionview Inside a Uitableviewcell - Dynamic Height
Get Indexpath of Uitableviewcell on Click of Button from Cell
Setting Custom Uitableviewcells Height
How to Use Uiview Autoresizingmask Property Programmatically