The UIView Class

The UIView class represents a rectangular area and is responsible for managing the content in that area. This includes drawing and animation, subview layout and management, and event handling.


Before looking at view properties, we need to understand structs.

A struct is a type defined in the C language for a structured record. Basically, it is a set of named attributes grouped into a single object. Think of it like a class that only contains public variables.

A struct definition looks like this:

You can then create a new variable of this type and access its member attributes using the dot notation.

When creating struct variables in Objective-C, we don’t use pointers but reference the objects directly.

View Properties

This is just a float. All coordinates and dimensions in views are of type CGFloat.

A struct made up of two CGFloats: x and y.
You can use the CGPointMake(CGFloat x, CGFloat y) function to create a CGPoint.

A struct made up of two CGFloats: width and height.
You can use the CGSizeMake(CGFloat width, CGFloat height) function to create a CGSize.

A struct made up of a CGPoint for the origin and a CGSize for the size. The origin (0, 0) of a view’s coordinate system is top left.
You can use the CGRectMake(CGFloat x, CGFloat, y, CGFloat width, CGFloat height) function to create a CGRect.

UIView has three properties used to position it in the space.

@property (nonatomic) CGRect bounds
The view’s location and size in its own coordinate system.

@property (nonatomic) CGRect frame
The view’s location and size in its superview’s coordinate system.

@property (nonatomic) CGPoint center
The view’s center point in its superview’s coordinate system. Setting this property changes the values in frame accordingly.

Use frame and center to position the view in the hierarchy. Use bounds inside your view’s implementation.


A view has only one superview (parent view), accessed using the superview property.

A view has zero or more subviews, accessed using the subviews array. The subviews are drawn in the same order as their index in the array (the later ones are drawn on top).

The topmost UIView is the UIWindow. There is usually only one UIWindow in an iOS app.

The view hierarchy is usually constructed graphically in the Interface Builder. However, it can also be done in code.

- (id)initWithFrame:(CGRect)aRect
the designated UIView initializer. Use it with alloc to create a view.

- (void)addSubview:(UIView *)view
adds the passed UIView to the end of the array. It is drawn on top of the other subviews.

- (void)removeFromSuperview
removes the UIView this method is called on from its superview.

  1. struct (C programming language). Wikipedia, Sep 12 2012.
  2. Paul Hegarty. Lecture 4 Slides. iPad and iPhone Application Development. Stanford, Nov 14 2011.

Comments are closed.