A tree-based hierarchy orders what you see
on your iPhone screen. Starting with the main window, views are laid out
in a specifically hierarchical way. All views may have children, called
subviews. Each view, including the window, owns an ordered list of
these subviews. Views might own many subviews; they might own none. Your
application determines how views are laid out and who owns whom.
Subviews display onscreen in order, always from
back to front. This works something like a stack of animation
cells—those transparent sheets used to create cartoons. Only the parts
of the sheets that have been painted show through. The clear parts allow
any visual elements behind that sheet to be seen. Views too can have
clear and painted parts, and can be layered to build a complex
presentation.
Figure 1 shows a little of the layering used in a typical window. Here the window owns a UINavigationController-based
hierarchy. The elements layer together. The window (represented by the
empty, rightmost element) owns a navigation bar, which in turn owns two
subview buttons (one left and one right). The window also owns a table
with its own subviews. These items stack together to build the GUI.
Listing 1 shows the actual view hierarchy of the window in Figure 6-1. The tree starts at the top UIWindow
and shows the classes for each of the child views. If you trace your
way down the tree, you can see the navigation bar (at level 2) with its
two buttons (each at level 3) and the table view (level 4) with its two
cells (each at level 5). Some of the items in this listing are private
classes, automatically added by the SDK when laying out views. For example, the UILayoutContainerView is never used directly by developers. It’s part of the SDK’s UIWindow implementation.
The only parts missing from this listing are the
dozen or so line separators for the table, omitted for space
considerations. Each separator is actually a UITableViewSeparatorView instance. They belong to the UITableView and would normally display at a depth of 5.
Listing 1. To Do List View Hierarchy
[ 0] UIWindow
--[ 1] UILayoutContainerView
----[ 2] UINavigationTransitionView
------[ 3] UIViewControllerWrapperView
--------[ 4] UITableView
----------[ 5] UITableViewCell
------------[ 6] UIView
--------------[ 7] UILabel
------------[ 6] UIButton
--------------[ 7] UIImageView
------------[ 6] UIView
----------[ 5] UITableViewCell
------------[ 6] UIView
--------------[ 7] UILabel
------------[ 6] UIButton
--------------[ 7] UIImageView
------------[ 6] UIView
----------[ 5] UIImageView
----------[ 5] UIImageView
----[ 2] UINavigationBar
------[ 3] UINavigationItemView
------[ 3] UINavigationButton
--------[ 4] UIImageView
--------[ 4] UIButtonLabel
------[ 3] UINavigationButton
--------[ 4] UIImageView
--------[ 4] UIButtonLabel