CFAlertViewController
CFAlertViewController
是一个库,帮助您在 iPad 和 iPhone 上显示和定制 警告、动作表单和通知。它提供屏幕旋转,并有自适应 UI 支持。CFAlertViewController 的 API 几乎与原生的 UIAlertController 相似,但功能更为丰富。
您还可以通过点击此处查看此库的 Android 版本:这里
使用案例类型
配置选项
需求
CFAlertViewController 支持iOS 8.0及以上版本的iPhone和iPad设备。它依赖于以下苹果框架:
- Foundation.framework
- UIKit.framework
推荐使用 Cocoapods 进行安装
我们假设您的 Cocoapods 已经配置好。如果您是 Cocoapods 初学者,请查看文档
- 在您的 Podfile 中添加
pod 'CFAlertViewController'
。 - 在终端中运行
pod install
命令安装 pod(在包含 Podfile 文件的文件夹中)。
使用源文件安装
在 Xcode 中打开下载的项目,然后将命名为 CFAlertViewController 的文件夹拖放到您的项目中(使用“产品导航视图”)。如果您在项目外部提取了代码归档,请确保在需要时选择复制项目。
用法
以上展示的警报可以通过以下代码片段轻松实现
// Create Alet View Controller
let alertController = 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: .notification,
didDismissAlertHandler: nil)
// Create Upgrade Action
let defaultAction = CFAlertAction(title: "UPGRADE",
style: .Default,
alignment: .right,
backgroundColor: UIColor(red: CGFloat(46.0 / 255.0), green: CGFloat(204.0 / 255.0), blue: CGFloat(113.0 / 255.0), alpha: CGFloat(1)),
textColor: nil,
handler: { (action) in
print("Button with title '" + action.title! + "' tapped")
})
// Add Action Button Into Alert
alertController.addAction(defaultAction)
// Present Alert View Controller
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?)
标题和消息
您可以在警告消息中设置自定义的标题和消息文本(如果不需要,传递 nil
标题颜色和消息颜色
您可以为警告消息中的标题和消息文本设置自定义的颜色(如果您想使用默认颜色值,传递 nil
对齐方式
您可以通过设置 textAlignment
属性来自定义标题和消息的对齐方式。使用以下值之一设置该属性:
NSTextAlignment.left,
NSTextAlignment.right,
NSTextAlignment.center,
NSTextAlignment.justified,
NSTextAlignment.natural
警告样式
您可以自定义警告的展示风格为警告或操作表。只需使用以下值之一设置 preferredStyle
CFAlertControllerStyle.alert,
CFAlertControllerStyle.actionSheet,
CFAlertControllerStyle.notification
背景样式
警告/操作表的背景(覆盖层)可以是模糊的(在需要隐藏背景的情况下,出于安全原因很有用)。默认值是 plain
CFAlertControllerBackgroundStyle.plain,
CFAlertControllerBackgroundStyle.blur
背景颜色
您可以使用 backgroundColor
在背景上轻触以取消
默认情况下,点击背景(覆盖层)后警报会消失。将shouldDismissOnBackgroundTap
属性更改为false
来禁用此功能。请记住,当将shouldDismissOnBackgroundTap
属性设置为true
时,用户可以使用交互式滑动手势来取消ActionSheet
或Notification
。
页眉/页脚
您可以在警报中添加页眉和页脚。使用自定义视图(UIView的子类)设置属性headerView
和footerView
。要排除这些属性,可以将它们设置为nil。
- 您可以在此示例中使用页眉的一些例子(美元图像位于页眉中)
- 您可以在此示例中使用页脚的一些例子
回调
当警报/操作表被取消时,将调用一个块(CFAlertViewControllerDismissBlock类型)。您可以使用它来处理取消回调。它还会提供以下枚举中提到的取消原因:
CFAlertControllerDismissReason.none,
CFAlertControllerDismissReason.onActionTap
CFAlertControllerDismissReason.onBackgroundTap
CFAlertControllerDismissReason.onInteractiveTransition
操作
public convenience init(title: String?,
style: CFAlertActionStyle,
alignment: CFAlertActionAlignment,
backgroundColor: UIColor?,
textColor: UIColor?,
handler: CFAlertActionHandlerBlock?)
标题
您可以为要添加的动作按钮设置标题。
动作样式
配置要添加到警报视图中的动作按钮样式。使用以下动作样式之一设置上述方法的 style
属性。
CFAlertActionStyle.Default,
CFAlertActionStyle.Cancel,
CFAlertActionStyle.Destructive
动作对齐
配置添加到警报视图中的动作按钮的对齐方式。使用以下动作类型之一设置 CFAction 构造函数中的 alignment
属性。
CFAlertActionAlignment.justified, // Action Button occupies the full width
CFAlertActionAlignment.right,
CFAlertActionAlignment.left,
CFAlertActionAlignment.center
回调
当动作被点击时,将调用 CFAlertActionHandlerBlock 类型的块。
容器视图
您还可以在 containerView
属性中配置背景颜色或图层相关属性(例如,cornerRadius、borderColor、borderWidth)。
许可证
此代码在 MIT 许可证的条款和条件下分发。