iOS offers a rich set of buttons, sliders, switches, and other user interface elements for you to use in creating your applications.
These elements can be roughly divided into two main groups, views and controls.

Views provide the primary canvas and drawing functionality of your user interface. They also give your application the ability
to handle touch events.

Controls extend upon this functionality and provide a way for users to interact with your application by defining what is known as
the target-action mechanism: the ability for a control to send an action (method call) to a target (object) when an event (touch) occurs.

In this chapter, you’ll look at the various views and controls available in iOS and examine how to use them.

All the examples use the View-based Application template, with the code running in the view controller.

Views

A view is the common name given to instances of UIView. You can think of a view as your application’s canvas; in other words, if you are adding UI elements to your iPhone’s interface,
you are adding them to a view. All the UI elements discussed in this chapter are themselves subclasses of UIView and so inherit its properties and behavior.

The root level of your iPhone application interface consists of a single UIWindow to which you would typically add one or more views to work with, instead of using UIWindow directly.

Since UIView is a subclass of UIResponder, it can receive touch events. For most views, you’ll receive only a single-touch event unless you set the multipleTouchEnabled property to TRUE. You can determine whether a view can receive touch events by modifying its userInteractionEnabled property. You can also force a view to be the only view to receive touch events by setting the exclusiveTouch property to YES. (For more information on working with touch events, see Chapter 7, “Touches, Shakes, and Orientation.”)

You can also nest views within each other in what’s known as the view hierarchy. Child views are known as subviews, and a view’s parent is its superview.

Frames

Views are represented by a rectangular region of the screen called a frame. The frame specifies the origin (x, y) and size (width, height) of the view, in relation to its parent superview. The origin of the coordinate system for all views is the
upper-left corner of the screen .

Bounds

A view’s bounds are similar to its frame, but the location and size are relative to the view’s own coordinate system rather than those of its superview. In the previous example, the frame’s origin is {10,10}, but the origin of its bounds is {0,0}. (The width and height for both the frame and the bounds are the same.)

The console output illustrates this : After moving the view 25 pixels in the x direction (using the view’s center property), the frame origin is now {35,10}, whereas the bounds origin remains at {0,0}.

Recall that, in the project, the UIWindow is at the top level. The UIWindow has two subviews: the status bar and the main view 20 pixels below . The origin of the frame of the main view is actually {0,20}. (Remember, a view’s frame is in relation to its superview’s coordinate system.)

The solution to this problem is to use the bounds of the superview (Code Listing 4.2), which causes the view to correctly fill its superview.

TIP

You can use the NSStringFromCGRect() function to convert a CGRect into an NSString, making it useful for logging CGRects to the console via NSLog(). Other useful functions when dealing with CGRects are NSStringFromCGPoint() and NSStringFromCGSize().

The CGRectInset() function takes a source rectangle and then creates a smaller or larger rectangle with the same center point. In this example,
a negative value for the width and height creates a larger rectangle.

Close the animation block:

[UIView commitAnimations];

This will cause all of the settings within the animation block to be applied.

Build and run the application.

You should see the view grow in size over a period of one second. Code Listing 4.3 shows the completed code.

TIP

Try changing the setAnimationDuration: line to see how you can affect the speed of the animation.

TIP

Try setting some other properties on the view within the animation block (such as backgroundColor) to see what effect they have.

Autosizing

When a view changes size or position, you often want any subviews contained within the view to change size or position in
proportion to their containing superview. You can accomplish this by using a view’s autoresizing mask. Now let’s add a second subview inside the view you created in the previous exercise.

To add a subview:

Create a CGRect for the subview’s frame, again using the shortcut CGRectInset() function:

This time, the positive width and height values for the CGRectInset function make the new view smaller. To make them stand out, give it a different background color.

Build and run the application . The new subview starts off in the center of its superview, but then it remains “pinned” to its initial location as the animation
progresses and ends up in the top-left corner.

Code Listing 4.4 shows this code updated to use an autoresizing mask. Notice how you set all four margins of the subview using the bitwise
OR operator (the | symbol) between the constant values (Table 4.1). Notice also that even though the animation is specified on the superview, the subview still animates automatically .

Custom drawing

By default, the visual representation of a UIView is fairly boring. You can manipulate the size, background color, and alpha levels of the view, but not much else.

Luckily, it’s relatively simple to create your own UIView subclasses where you can implement custom drawing behavior. To see how this might be done, you’ll now learn how to create
a UIView subclass with rounded corners.

Open roundedCornerView.m, and modify your code to look like Code Listing 4.5.

Open UITestViewController.m, and replace all instances of UIView with roundedCornerView. Don’t forget to also import the header file for roundedCornerView.h at the top of the file. Code Listing 4.6 shows the updated code.

Build and run your application to see the result with rounded corners for the views .

As you can see, custom drawing happens in the drawRect: method of roundedCornerView. You set a couple of variables here—one to determine the width of the line you will be drawing and another to determine the
color.

By setting the color to the superview’s background color, you are essentially “erasing” any time you draw in the subview.

Transforms

You’ve already looked at resizing a view by increasing the width and height of its frame. Another way to perform the same
task is by using a transform.

A transform maps the coordinates system of a view from one set of points to another. Transformations are applied to the bounds of a view. In addition to scaling, you can also rotate and move a view using transforms.