UIView is the superclass for almost everything that appears on the screen. It inherits from UIResponder (which inherits from NSObject). Buttons, labels, editable text fields and images are all views. You can define your own subclass to do custom drawing for graphics or special layouts.

A UIView has a frame, the rectangle it occupies within the view that contains it. A typical initialization provides that frame,

CGRect viewRect = CGRectMake(10, 10, 100, 100);
UIView * myView = [[UIView alloc] initWithFrame:viewRect];

Once the view is created it is usually made a subview of another view,

[superView addSubView:myView];

Views overlap each other. The most recently added covers any behind it. There are several methods to change the order of views and remove them. When a view is visible, all its subviews are visible.

Properties of a UIView:
frame, center and bounds
alpha, hidden, opaque
background color
transform

Some of the attributes can be animated, for example, the alpha (transparency). A separate interface allows animated transforms like change of scale and rotation.

A drawRect method will do the actual drawing. It is called by the OS when any change to the view occurs (being hidden or unhidden, moved, uncovered, when the image changes, etc.) You will write the drawRect for a custom subclass, but you never call it directly. Leave that to the system. If you want to make the view draw itself, call setNeedsDisplay. The OS will call drawRect at the soonest opportunity. The subclasses in UIKit (UIButton, UILabel, etc.) have their own drawRect built in. You should not replace it.

UIView is a subclass of UIResponder. It can respond to touches. Using gesture recognizers multi-touch events, like pinches, can be handled.

The View Programming Guide.

The page was last updated Thursday, November 3, 2011 11:03 AM