Custom UIAlertView for iOS

It was a common pattern for iOS developers to create customised dialogs by attaching a subview to a standard UIAlertView. Since iOS7 however, Apple removed this feature and developers are left without a dialog pattern that matches the iPhone UI.

As a solution, I wrote an open source class to create an iOS8-style dialog which can be extended with any UIViews or buttons. The animations and the looks are copied too, and no images or other resources are needed.

You can just grab the open source code from Github now. In this article I will write about the implementation best practices and some background info.

Extend your current AlertView code to support iOS7 and iOS8

To create an AlertView with a custom subview, you probably wrote something similar to this:

And you did probably reset the frame in the delegate method willPresentAlertView too.

This was working on all previous iOS versions. On iOS7 however it will result in an empty dialog. What you have to do therefore is to fork the code here: display a standard UIAlertView on the old devices, and a CustomIOSAlertView on the new ones.

Forking the code is as simple as this:

For the new dialog, you would add something like the following:

Putting these two together, a full solution would look like this:

And that’s it! For the CustomIOSAlertView is a hack-free UIView, you don’t have to add anything to the willPresentAlertView delegate, just keep it as it is. For handling the iOS7 button clicks, you can use code blocks or delegates – please refer to the project’s readme.

How did we end up here?

Apple never officially supported the addSubview method of the UIAlertView. Their approach is to use this dialog only for a small subset of functions, like a plain text input. This is as easy to do as setting the style of the UIView to UIAlertViewStylePlainTextInput.

However, UI/UX designers like to use the dialog pattern for other use cases as well. An example is to show a simple image to the user, or display a progress bar while the user is waiting for a background download to be finished.

Until the previous iOS version the addSubView method worked fine, and developers used it as a best practice for these dialogs patterns. That’s why Apple’s move left app publishers in a bad position: before rolling out the updated version of our apps, in some cases we have to find a completely new user journey.

This is where the CustomIOSAlertView comes handy: we can roll out the iOS7-support quickly (and start up the user experience think thank in the background).

(Free code and source from Github. Opinions, debate: @wimagguc on Twitter.)

j j j