CFAlertViewController
is a library that helps you display and customise alerts and action sheets on iPad and iPhone. It offers screen rotation as well as an adaptive UI support. CFAlertViewController’s functionality is almost similar to the native UIAlertController.
CFAlertViewController works on devices (iPhone and iPad) with iOS 8.0+. It depends on the following Apple frameworks:
- Foundation.framework
- UIKit.framework
We assume that your Cocoapods is already configured. If you are new to Cocoapods, have a look at the documentation
- Add
pod 'CFAlertViewController'
to your Podfile. - Install the pod(s) by running
pod install
in terminal (in folder wherePodfile
file is located).
Open the downloaded project in Xcode, then drag and drop folder named CFAlertViewController onto your project (use the "Product Navigator view"). Make sure to select Copy items when asked if you extracted the code archive outside of your project.
The above shown alert and actionsheet can easily be implemented using the code snippet given below by some small tweaks
// Create Alert
var alertController: CFAlertViewController = CFAlertViewController(title: "You've hit the limit",
message: "Looks like you've hit your daily follow/unfollow limit. Upgrade to our paid plan to be able to remove your limits.",
textAlignment: .left,
preferredStyle: .alert) { (didTapBackground) in
if (didTapBackground) {
// Handle background tap here
}
}
var defaultAction = CFAlertAction(title: "UPGRADE",
style: .Default,
alignment: .justified,
backgroundColor: UIColor(red: CGFloat(46.0 / 255.0), green: CGFloat(204.0 / 255.0), blue: CGFloat(113.0 / 255.0), alpha: CGFloat(1)),
textColor: UIColor.white) { (action) in
print("Button with \(action.title) title tapped")
}
// Add Action Button Into Alert
alertController.addAction(defaultAction)
// Present Alert View Controller
self.present(alertController, animated: true, completion: nil)
public convenience init(title: String?,
titleColor: UIColor?,
message: String?,
messageColor: UIColor?,
textAlignment: NSTextAlignment,
preferredStyle: CFAlertControllerStyle,
headerView: UIView?,
footerView: UIView?,
didDismissAlertHandler dismiss: CFAlertViewControllerDismissBlock?)
You can set a custom title and message text in the alert (pass nil
if you don’t need them).
You can set a custom title and message text color in the alert (pass nil
if you want to use Default color values).
You can customise alignment of the title and message. Set the textAlignment
property with one of the following values :
NSTextAlignment.left,
NSTextAlignment.right,
NSTextAlignment.center
Presentation style of the alert can be customised as Alert or Action sheet. Just set the preferredStyle
property with one of the following values :
CFAlertControllerStyle.actionSheet,
CFAlertControllerStyle.alert
Background (overlay) of alert/action sheet can be blurred (Useful for security reasons in case the background needs to be hidden). Default value is plain
. You can customize the blur style using backgroundBlurView
property of type UIVisualEffectView. Update backgroundStyle
property with one of the following enum values:
CFAlertControllerBackgroundStyle.plain,
CFAlertControllerBackgroundStyle.blur
You can change the background (overlay) color of the alert/actionsheet using the property backgroundColor
.
By default, the alert gets dismissed after tapping on the background (overlay). Change shouldDismissOnBackgroundTap
property to false
to disable it.
You can add header and footer to the alert. Set properties headerView
and footerView
with custom views (subclass of UIView). You can pass nil to this properties to opt them out.
- Some examples where you can make the use of header in alert (the dollar image is in header)
- Some examples where you can make the use of footer in alert
A block (of type CFAlertViewControllerDismissBlock) gets called when the Alert / Action Sheet is dismissed. You can use it to handle call back.
public convenience init(title: String?,
style: CFAlertActionStyle,
alignment: CFAlertActionAlignment,
backgroundColor: UIColor?,
textColor: UIColor?,
handler: CFAlertActionHandlerBlock?) {
You can set the title of action button to be added.
Configure the style of the action button that is to be added to alert view. Set style
property of the above method with one of the following Action style
CFAlertActionStyle.Default,
CFAlertActionStyle.Cancel,
CFAlertActionStyle.Destructive
Configure the alignment of the action button added to the alert view. Set alignment
property of CFAction constructor with one of the following action types
CFAlertActionAlignment.justified, // Action Button occupies the full width
CFAlertActionAlignment.right,
CFAlertActionAlignment.left,
CFAlertActionAlignment.center
A block (of type CFAlertActionHandlerBlock) gets invoked when action is tapped.
You can also configure the background color or layer related properties (e.g. cornerRadius, borderColor, borderWidth) in the containerView
property.
This code is distributed under the terms and conditions of the MIT license.