与 UIAlertController 类似风格的 UI 控件,包含 Alert 和 ActionSheet 以及无边距的 ActionSheet 类型.支持添加自定义视图元素,多种AlertAction可供选择,并容易扩展实现新的 AlertAction UI样式,支持优先级队列
ADAlertController
是一个与UIAlertController
类似风格的 UI 控件,包含 Alert
和 ActionSheet
以及无边距的 ActionSheet(ADAlertControllerStyleSheet)
等 UI类型.与UIAlertController
有相似的 API.支持 iOS9+.并支持以下扩展功能:
AlertAction
可供选择,并容易扩展实现新的 AlertAction
UI样式使用CocoaPods
:
在Podfile
中添加以下语句
pod ‘ADAlertController’
手动安装:
下载仓库代码,将ADAlertController
文件夹下所有的文件拖到你的项目工程中
PS:ADCustomAlertContentView
文件夹并非ADAlertController
要求的源码,而是实现自定义内容视图的一点建议.若有其他更好的用法,欢迎提供意见.
若在使用过程中有任何问题,欢迎提Issue
针对演示图片中的第一张图出现的第一个例子为例,初始化代码如下:
ADAlertAction *cancelAction = [ADAlertAction actionWithTitle:@"取消" style:ADActionStyleCancel handler:^(__kindof ADAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}];
ADAlertAction *sureAction = [ADAlertAction actionWithTitle:@"确定" style:ADActionStyleDefault handler:^(__kindof ADAlertAction * _Nonnull action) {
NSLog(@"点击了确定");
}];
ADAlertController *alertView = [[ADAlertController alloc] initWithOptions:nil title:@"这里是标题" message:nil actions:@[cancelAction,sureAction]];
[alertView show];
ADAlertController
使用指定构造方法来生成实例:-initWithOptions
.message
第一个参数是ADAlertControllerConfiguration
对象类型,可以为 nil.
ADAlertControllerConfiguration
用来配置一些 UI 样式.其中的preferredStyle
属性就是来指定具体的 UI 样式,有以下几个枚举类型:
ADAlertControllerStyleAlert
:与UIAlertControllerStyleAlert
类似ADAlertControllerStyleActionSheet
:与UIAlertControllerStyleActionSheet
类似ADAlertControllerStyleSheet
:与UIAlertControllerStyleActionSheet
类似,只是左右以及底部的边距都是 0.preferredStyle
属性一经初始化,不得更改.
ADAlertControllerConfiguration
使用指定构造方法-initWithPreferredStyle:
第二,第三个参数分别表示标题和内容信息,可为 nil.
第三个参数是包含的按钮,可为 nil.不同于UIAlertController
的添加按钮方法,这里添加按钮采用数组的方式.
对于ADAlertControllerStyleAlert
类型,这个数组的内容就是警告框最终包含的按钮.
对于ADAlertControllerStyleActionSheet
和ADAlertControllerStyleSheet
类型,若想添加取消按钮,不是将取消按钮放到最后一个,而是应该调用-addActionSheetCancelAction:
来添加.
ADAlertAction
是具体的按钮类,用来生成标题或者图片的按钮,标题或图片只能二选一显示,若同时配置二者,则优先显示标题.构造方法为+ actionWithTitle
style
configuration:
其中handler
为点击事件回调.configuration
为 UI 配置相关.
若需要实现自己的按钮 UI 样式,可以参考ADAlertImageAction
的实现.
另外还提供了两个ADAlertAction
的子类:
ADAlertGroupAction
:可将多个ADAlertAction
作为一个按钮添加,内部的各个按钮平分宽度ADScrollableGroupAction
:ADAlertGroupAction
的子类,当按钮个数多个时,可滑动显示显示时可直接调用-show
方法.想主动隐藏可以调用-hiden
方法.若想触摸背景也自动隐藏,可以设置ADAlertControllerConfiguration
的hidenWhenTapBackground
属性为YES
.
若想进入优先级队列,可以调用-enqueue
设置ADAertController
的contentView
为你需要的UIView
对象即可,需要自己设置高度约束.建议的方法为[contentView.heightAnchor constraintEqualToConstant:100].active = YES
.
ADAlertControllerStyleAlert
类型时,是显示在标题之上.
ADAlertControllerStyleActionSheet
和ADAlertControllerStyleSheet
类型时,显示在message
之下
具体用法可参考示例中的ADAdvertView
和ADPickerView
当为ADAlertControllerStyleAlert
类型时可以添加textField
.具体参考示例中的”添加输入框的警告框”
目前添加UITextField 暂未适配键盘遮挡问题
若需要显示多个AlertController
又难以管理各个AlertController
的显示顺序时,可以设置优先级属性alertPriority
并调用入队列方法-enqueue
.在内部有个优先级队列管理对象(ADAlertControllerPriorityQueue
)会自动管理下一个将要显示的AlertController
对象
使用优先级队列有以下注意点:
alertPriority
是NSUInteger
类型,虽只有三个枚举值,但可以设置任何有效的NSUInteger
值.AlertController
仅针对某个目标控制器显示.具体参考示例中的”指定在某个控制器显示”