Alerts have been around since iOS was iPhone OS. First in the form of UIAlertView, and now through UIAlertController (Bite #44). The built-in system alerts work great, but sometimes we want something fancier 🎩. Today we'll check out PMAlertController from Paolo Musolino, a library that allows us to easily display great looking alerts in our app. Let's take a look.
PMAlertController is essentially a fancier replacement for the styles that UIKit's UIAlertController already provides.
PMAlertController two styles of alerts: "Alert" and "Walkthrough".
The "Alert" Style is 270 points wide (matching the system's alerts):
The "Walkthrough" Style is almost the full width of the screen and is appropriate for more in-depth alerts/dialogs (such as those for explaining why our app is requesting permission for some of the user's data):
Using PMAlertController is almost just like UIAlertController
.
We're given a title, description and image to work with:
let alertVC = PMAlertController(
title: "Locate your device",
description: "Enables access to your...",
image: UIImage(named: "flag.png"),
style: .alert
)
alertVC.addAction(PMAlertAction(
title: "Cancel", style: .cancel,
action: { in }
))
alertVC.addAction(PMAlertAction(
title: "Allow", style: .default,
action: { in }
))
present(alertVC, animated: true, completion: nil)
We instantiate the view controller, add some actions (each with their own closure handler that will be called when the action is tapped), and present it.
Neat!
Libraries like PMAlertController can help us easily add some polish to our apps, but they can also be a great way to learn about creating custom controls in general.
Learn more about PMAlertController at git.io/pmalert