iOS SDK: UIAlertView Custom Graphics

The UIAlertView class is easy to implement, but unless you get under the hood and tinker around it will look the same each time. This Quick Tip shows you how to create custom graphics programmatically and apply them to an alert. Changing the graphics in your app may be just the ticket for a fresh and engaging design!

Step 1: Set Up the Xcode File

Launch Xcode and create a new project by clicking File > New > New Project. We want to create an
iOS Empty Application. Name your product "Custom Alert" and enter a Company Identifier of your choosing. Select "iPhone" from the "Device Family" menu and remove any checks in front of "Use Core Data," "Use Automatic Reference Counting," and "Include Unit Tests." Click "Next" and choose a location to save your project. Make sure the box next to "Source Control" is not checked and click "Create" to make your project.

Step 2: Create a UIAlertView

We first want to subclass UIAlertView. Click File > New > New File to create a new subclass. Choose an iOS Cocoa Touch Objective-C class and click "Next." Name your class "CustomAlertView." and choose UIView from the "Subclass of" menu. Before clicking "Next.", choose the "CustomAlert" folder from the "Groups" menu and make sure the "CustomAlert" is checked in the "Targets" box. Click "Create" to add the new subclass.

The newly-created class is a UIView class, but we need it to inherit from UIAlertView. Click on the "CustomAlertView.h" file and change the inheritance to UIAlertView.

In the AppDelegate header file, import the "CustomAlertView.h" file and conform to the UIAlertViewDelegate protocol.

Navigate to the AppDelegate implementation file and include the instance method alertView:clickedButtonAtIndex:.

The buttonIndex is used to determine which button was pressed.

Now, insert the following code to instantiate the alert object in the application:didFinishLaunchingWithOptions: method.

Note that the delegate is set to self in the init method in order to receive the callback when a button is pressed.

If you build and run the app right now, you will get the typical graphics of a standard UIAlertView.

Step 3: Customize the Graphics

Let's remove the standard graphics and customize the UIAlertView.

Overriding layoutSubviews:

First, we need to override UIView's layoutSubviews: method in the "CustomAlertView.m" file where we will hide the blue background and find the labels to change the appearance of the text.

Because layoutSubviews: is called after the object is initialized, but before the object is drawn, you have the opportunity to add code that will provide additional customization before it appears on the screen. In the above code, we use fast enumeration to iterate through the array of existing subviews of the alert looking for the UIImageView, which holds the background and two UILabels which hold the text. By using the convenience method isMemberOfClass:, a Boolean value of true or false is returned depending on whether the subview is an instance of the specified class. When the classes are located, the UIImageView is hidden and the UILabels' text colors, shadow colors, and shadow offsets are set.

Overriding drawRect

Importing pre-made vector graphics can be problematic. Between changing screen resolutions and organizing files, importing images tends to be more work than it is worth in some cases. If you are looking for flexible, pixel-perfect graphics, overriding the view's drawRect: method is usually the best solution.

The graphics for the UIAlertView created by the following code provide a subtle difference in color, shadow, and texture, but keep the same general shape of a standard alert. Insert the sections of code explained in this tutorial into the drawRect: method in a subclass of UIAlertView.

Get a Reference to the Current Graphics Context

The UIGraphicsGetCurrentContext() function returns a reference to the current view's graphics context, allowing you to make changes to its state.

Create a Base Shape

We start by creating a basic shape with rounded corners. Notice that the parameters for the shape are based on the current bounds of the UIAlertView. This allows the shape to be drawn in a consistent manner with the alert's bounds, compensating for any changes that occur. Two important components when creating this shape are cornerRadius and inset. The cornerRadius determines the roundness of the corners, while the inset defines how far inside the bounds the shape is drawn. Finally, we create the shape with a UIBezierPath constructed from the convenience method bezierPathWithRoundedRect: cornerRadius:.

Accessing the CGPath property of the newly created UIBezierPath object allows it to be used in the context.

Add a Fill and Outer Drop Shadow

When drawing in Core Graphics, think in terms of layering pieces on top of each other. In this project, the first layer consists of drop shadows because they are the outermost and bottom layer. We set the parameters of the first graphics state, specifically its shape, fill color, and shadow color. Then we draw the context using CGContextDrawPath() and the CGPathDrawingMode kCGPathFill which will essentially create the exterior drop shadow.

Clip the Context

Now we want to draw inside the base shape. In order to accomplish this, we need to clip the context to the shape prior to drawing the next state. Anything drawn afterward but before we call CGContextRestoreGState() stays inside the clipping area and doesn't cover the exterior drop shadow.

Draw a Gradient

Next, we add a subtle gradient to the background. In order to draw a gradient, we will need a gradient reference which specifies the colors and their locations within the gradient. The startPoint and endPoint of the gradient are based on the bounds of the alert view, covering the entire alert without going outside of the clipping area. Note that we've released the CGColorSpaceRef and CGGradientRef now that we are done with them. In general, when you see a Core Graphics function call with the word &quotcreate&quot in it, you'll need to release the reference when you are finished.

Create a Hatched Background

The next bit of code deals with the hatched diagonal lines behind the buttons. In order to get the lines to draw only underneath the buttons we need to create a new rectangle shape, clip the context to the rectangle, and add the hatched lines to the context. The buttonOffset is the height of the space for the hatched lines to be drawn. The moving pattern created by the for loop is based on the alert view's bounds. To change the appearance of the hatched lines simply alter the spacer CGContextSetLineWidth(), or the hatchFrame values.

Draw a Separating Line

Now, we want to draw a separating line between the buttons and the labels. Creating the line using CGPathCreateMutable draws it using the specified point locations. The line's location within the alert view is based on the buttonOffset and spans its width. Once again, we use a function call containing the word "create," CGPathCreateMutable(). Therefore, we release the CGMutablePathRef one after we're done.

Create an Inner Shadow

To create the appearance of a raised border around the alert view, we draw the original UIBezierPath with a shadow. The path will continue to be clipped by the first call to clip the context which results in the shadow appearing only on the inside of the stroked line. To change the size of the inner shadow, set the blur parameter inside CGContextSetShadowWidthColor to a different number.

Redraw the Path to Avoid Pixilation

Rounded corners have a tendency to appear pixilized once they have been clipped, so our last bit of code redraws the outer line to ensure it is crisp and clean. Before we redraw the outer line, we call CGContextRestoreGState() which releases our first clipping area, so we can draw on top of the original line.

Step 4: Test the UIAlertView

Run the app on the simulator or on your device to view the UIAlertView and its custom graphics.


Creating graphics using Core Graphics is about subtlety and layering of effects. There are plenty of graphical elements that can be adjusted when customizing UIAlertView , and this tutorial only covers a small portion of them. But the point is that these elements can all be adjusted and created programmatically to enhance the user's experience.



Related Articles