views:

1814

answers:

3

Hi,

I'm dragging things around in the Interface Builder... I'd like to specify whether an image is in front (like an indicator) or behind of a button (like a background). I don't see any z-index property as I'm used to seeing on other environments. If there isn't a z-index property what is the best way to go about what I'm trying to accomplish? Thanks!

+3  A: 

In XCode development, a UI element, or "view" is in front of another view when it is a subview of that view. For example, if view B is a background and view C is a control, to place the control above the background (i.e. closer to the user), you would make view C a subview of view B. In Interface Builder, this is accomplished by dragging the control into the background.

Essentially, you are looking at a tree structure, with the views in the background being near the root of the tree, and views in the foreground (closer to the user) being near the leaves of the tree.

The Windows and Views document from Apple's iPhone developer documentation may help to clear things up.

Note 1: You should almost never overlap individual controls, such as buttons and text fields. Doing so goes against Apple's user interface guidelines. You can, of course, still do this if you want to, but you need to be aware that you are stepping out of the safety zone. If you are simply writing a "normal" iPhone application, your best bet is to stick to Apple's way of doing things.

Note 2: If, for some reason, you do need things to overlap in a specific way, you can make use of CALayer objects to keep everything properly ordered. CALayer objects are part of Apple's Core Animation technology.

e.James
+1 Concisely explained
TechZen
I see what you're saying, but what if you had multiple controls within the same view. For example, say I have a button MyButton inside a view MyView, but I wanted an Image to be over the button (and yes, I want an image over a button, I _don't_ want to use button's inherent image property). Is there a possible way to do this or no?
Shnitzel
Yes, it *can* be done, although it may be a bit more difficult than simple drag-and-drop. See http://stackoverflow.com/questions/466297/is-there-a-proper-way-to-handle-overlapping-nsview-siblings
e.James
hmm, apple needs to get their act together about this one. z-index is the way to go. this is primitive. thanks for your help though!!
Shnitzel
If you want to do it properly, using CALayers is the way to go. In IB, if you enable layers for your base view (the one that will contain the overlapping siblings), then dragging the image on top of the button should just work. I think you will have to add the button first, and then add the image, and then drag the image on top. Also, keep in mind that you may have to jump through some hoops to get "click-through" to work.
e.James
You are welcome for the help, but I wouldn't be too quick to judge Apple on this one. A tree structure for views allows for very efficient drawing operations because the runtime can quickly split up the canvas into separate regions that need to be redrawn.
e.James
It may take a small amount of adjustment during development, but it pays off big time when the user is actually running your app, and *that* is when it really matters.
e.James
I've just realized that the question I linked to in my first comment is my own, from way back in the day, when I was learning this stuff for the first time! I guess we all have to ask these questions when we're getting started :)
e.James
+2  A: 

I achieved what I wanted by clicking on a ui element (button, image, text, etc) and going to the Layout menu (at the top of screen) and then I used "bring to front", "send to back", etc.

Shnitzel
+1... it's hysterical to me that there are all these complicated explanations, yet the right answer was not submitted. (I guess Kendall did actually mention the menu items, but didn't go into where they were located.) Purely speculation, but I imagine CALayer objects MUST have some concept of z-index.
livingtech
+4  A: 

There is a Z ordering without using subviews. For one thing there are menu options for "send to front" and "send to back". Also however, if you look at the elements in your view as a tree of elements, you can re-order them there (rather than in the view itself) just by dragging.

Subviews are great for grouping but not as useful for ordering (except that the whole "set" stays at the same level).

Kendall Helmstetter Gelner